@@ -14,16 +14,20 @@ class IMUINotificationMessageCell: IMUIBaseMessageCell {
1414 var titleLable = UILabel ( )
1515 var backView = UIView ( )
1616 let screenW = UIScreen . main. bounds. size. width
17+ // let labelGest = UITapGestureRecognizer.init()
1718
1819 override init ( frame: CGRect ) {
1920 super. init ( frame: frame)
2021 titleLable. textColor = UIColor . white
2122 titleLable. font = UIFont . systemFont ( ofSize: 12 )
22- titleLable. textAlignment = NSTextAlignment . center
23+ // titleLable.font = UIFont.init(name: "helvetica", size: 12)
24+ titleLable. numberOfLines = 0
25+ titleLable. isUserInteractionEnabled = true
2326 backView. backgroundColor = UIColor . init ( red: 206 / 255.0 , green: 206 / 255.0 , blue: 206 / 255.0 , alpha: 1 )
2427 backView. layer. cornerRadius = 5
2528 backView. clipsToBounds = true
2629 backView. addSubview ( titleLable)
30+ // labelGest.addTarget(self, action: #selector(self.clickTapValidation(sender:)))
2731 bubbleView. addSubview ( backView)
2832 }
2933
@@ -40,12 +44,25 @@ class IMUINotificationMessageCell: IMUIBaseMessageCell {
4044 let layout = message. layout
4145 let tmpDict = message. customDict
4246 let strTitle = tmpDict. object ( forKey: " tipMsg " ) as! String
43- self . titleLable. text = strTitle
4447 let titleSize = sizeWithFont ( font: UIFont . systemFont ( ofSize: 12 ) , text: strTitle, maxWidth: layout. bubbleFrame. size. width*0. 8 )
48+ if titleSize. height < 20 { //一行
49+ titleLable. textAlignment = NSTextAlignment . center
50+ } else {
51+ titleLable. textAlignment = NSTextAlignment . left
52+ }
53+
4554 let titleX = ( layout. bubbleFrame. size. width - titleSize. width - 10 ) * 0.5
46- let titleY = ( layout. bubbleFrame. size. height - titleSize. height - 10 ) * 0.3
47- self . backView. frame = CGRect ( origin: CGPoint ( x: titleX, y: titleY) , size: CGSize ( width: titleSize. width+ 10 , height: titleSize. height+ 10 ) ) ;
48- self . titleLable. frame = self . backView. bounds
55+ let titleY = ( layout. bubbleFrame. size. height - titleSize. height - 10 ) * 0.5
56+ self . backView. frame = CGRect ( origin: CGPoint ( x: titleX, y: titleY) , size: CGSize ( width: titleSize. width+ 10 , height: titleSize. height+ 10 ) )
57+ self . titleLable. frame = CGRect ( origin: CGPoint ( x: 5 , y: 5 ) , size: CGSize ( width: titleSize. width, height: titleSize. height) )
58+ let attString = NSMutableAttributedString . init ( string: strTitle)
59+ // if strTitle.hasSuffix("发送朋友验证"){
60+ // self.titleLable.addGestureRecognizer(labelGest)
61+ // let tmpArr = rangesOf(searchString: "发送朋友验证", inString: strTitle)
62+ // let tmpR:NSRange = tmpArr.lastObject as! NSRange
63+ // attString.addAttribute(NSForegroundColorAttributeName, value: UIColor.init(red: 35/255.0, green: 141/255.0, blue: 250/255.0, alpha: 1), range: tmpR)
64+ // }
65+ self . titleLable. attributedText = attString
4966
5067 }
5168 func sizeWithFont( font : UIFont , text : String , maxWidth: CGFloat ) -> CGSize {
@@ -60,6 +77,48 @@ class IMUINotificationMessageCell: IMUIBaseMessageCell {
6077
6178 return rect. size
6279 }
80+
81+ func rangesOf( searchString : String , inString : String ) -> NSMutableArray {
82+ let results : NSMutableArray = NSMutableArray . init ( )
83+ let nsInStr = inString as NSString
84+ var searchRange = NSMakeRange ( 0 , nsInStr. length)
85+ while nsInStr. range ( of: searchString, options: NSString . CompareOptions ( rawValue: 0 ) , range: searchRange) . location != NSNotFound {
86+ let tmpRange = nsInStr. range ( of: searchString, options: NSString . CompareOptions ( rawValue: 0 ) , range: searchRange)
87+ results. add ( tmpRange)
88+ searchRange = NSMakeRange ( NSMaxRange ( tmpRange) , nsInStr. length - NSMaxRange( tmpRange) )
89+ }
90+ return results
91+ }
92+
93+ func clickTapValidation( sender : UITapGestureRecognizer ) {
94+ let touchPoint = sender. location ( in: self . titleLable)
95+ let path : CGMutablePath = CGMutablePath ( )
96+ path. addRect ( CGRect ( x: 0 , y: 0 , width: self . titleLable. frame. size. width, height: self . titleLable. frame. size. height) )
97+ let framesetter = CTFramesetterCreateWithAttributedString ( self . titleLable. attributedText!)
98+ let ctframe = CTFramesetterCreateFrame ( framesetter, CFRangeMake ( 0 , 0 ) , path, nil )
99+ //获取行数
100+ let lines : NSArray = CTFrameGetLines ( ctframe)
101+ print ( " line: \( CFArrayGetCount ( lines) ) line: \( self . titleLable. numberOfLines) height: \( self . titleLable. frame. size. height) " )
102+ let numOfLine : NSInteger = self . titleLable. numberOfLines > 0 ? min ( self . titleLable. numberOfLines, CFArrayGetCount ( lines) ) : CFArrayGetCount ( lines)
103+ if numOfLine != 0 {
104+ var lineOrigins : [ CGPoint ] = Array ( repeating: . zero, count: numOfLine)
105+ CTFrameGetLineOrigins ( ctframe, CFRangeMake ( 0 , numOfLine) , & lineOrigins)
106+ for i in 0 ..< numOfLine{
107+ var baseLineOri = lineOrigins [ i]
108+ baseLineOri. y = self . titleLable. frame. height - baseLineOri. y
109+ let line : CTLine = lines [ i] as! CTLine
110+ var ascent : CGFloat = 0.0
111+ var descent : CGFloat = 0.0
112+ var leading : CGFloat = 0.0
113+ let lineWidth : CGFloat = CGFloat ( CTLineGetTypographicBounds ( line, & ascent, & descent, & leading) )
114+ let lineFrame = CGRect ( x: baseLineOri. x, y: baseLineOri. y- ascent, width: lineWidth, height: ascent+ descent)
115+ if lineFrame. contains ( touchPoint) {
116+ let index = CTLineGetStringIndexForPosition ( line, touchPoint)
117+ print ( " index: \( index) " )
118+ }
119+ }
120+ }
121+ }
63122}
64123
65124
0 commit comments