swift学习也快有一个月了。文章也写了不少。今天来发布一个小DEMO。当操刀练手了。
主要写一个小控件,实现功能是类拟IPHONE手机打开AppStore 中的详细信息中的内容,先显示一部分,点击更多时,再加载完成。
目前只对单文本字体作处理,对于富文本的并不行。因此作为swift的操刀之作,还算是处女作吧。。。。。
源码:
整个控件的源码在:http://download.csdn.net/detail/fengsh998/7552229
效果:
其中这个“更多“的位置可以自行调整,当点击更多时,就会展示全部:
核心代码:
- //morebtn
- funcdrawMoreInRect(moreRect:CGRect)
- {
- varctx=UIGraphicsGetCurrentContext();
- CGContextSaveGState(ctx);
- varraduis=CGRectGetHeight(moreRect)/2.0
- varmaxX=CGRectGetMaxX(moreRect)
- varminX=CGRectGetMinX(moreRect)
- varmaxY=CGRectGetMaxY(moreRect)
- varminY=CGRectGetMinY(moreRect)
- CGContextMoveToPoint(ctx,minX,minY)
- CGContextAddArcToPoint(ctx,maxX,minY,maxY,raduis)
- CGContextAddArcToPoint(ctx,raduis)
- CGContextClosePath(ctx)
- ifselectedHightlight
- {
- CGContextSetRGBFillColor(ctx,29/255.0,158/255.0,245/255.0,1.0)
- }
- else
- CGContextDrawPath(ctx,kCGPathFill)//根据坐标绘制路径
- CGContextRestoreGState(ctx)
- overridefuncdrawRect(rect:CGRect)
- varattributedString=NSMutableAttributedString(string:self.text)
- letlen=countElements(String(self.text))
- letfname=self.font.fontName
- //StringconverttoCFString
- //letcfstr:CFString=reinterpretCast(fname.withCString(getenv))
- letmfont=self.font
- varattributes=NSMutableDictionary()
- attributes.setObject(mfont,forKey:kCTFontAttributeName)
- attributedString.addAttributes(attributes,range:NSMakeRange(0,len))
- self.attributedText=attributedString;
- varframesetter=CTFramesetterCreateWithAttributedString(attributedString)
- varPath=CGPathCreateMutable()
- varrectWidth=self.bounds.size.width
- CGPathAddRect(Path,nil,CGRectMake(0,rectWidth,CGFLOAT_MAX))
- varframe=CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0),Path,nil)
- varrows=CTFrameGetLines(frame)
- iflethasRows=rows?
- //获取frame中的行数
- varnumberOfLines=CFArrayGetCount(rows)
- //println("rows=\(numberOfLines)")
- varfontLineHeight=self.font.lineHeight
- vartextOffset=self.font.ascender
- varctx=UIGraphicsGetCurrentContext()
- CGContextSaveGState(ctx)
- //距左上位置设置
- CGContextTranslateCTM(ctx,0)
- CGContextSetTextMatrix(ctx,CGAffineTransformMakeScale(1,-1))
- forvarlineNumber=0;lineNumber<numberOfLines;lineNumber++
- varonlyline=CFArrayGetValueAtIndex(rows,lineNumber)
- varflush:Double=0.0
- varonlycline:CTLine=reinterpretCast(onlyline)
- varpenOffset=CTLineGetPenOffsetForFlush(onlycline,flush,rect.size.width)
- CGContextSetTextPosition(ctx,penOffset,textOffset);
- //println("第\(lineNumber)行\(onlycline)")
- //获取第onlycline行中的字形数
- //varnums=CTLineGetGlyphCount(onlycline)
- //println("本行的字符数\(nums)")
- ifletmp=morepoint
- varposline=mp.row
- varposclm=mp.column
- varbool_lastline=(posline==numberOfLines-1)?true:false
- iflineNumber==posline&&!self.expand
- vartruncatedString=NSAttributedString(string:"\u2026")
- vartoken=CTLineCreateWithAttributedString(truncatedString)
- varrange=CTLineGetStringRange(onlycline)
- varmaxlengthInline=range.length
- posclm=min(posclm,maxlengthInline)
- //获取onlycline中一黄有几个CTRun,一般情况下,一行中只有一个CTRun
- varruns=CTLineGetGlyphRuns(onlycline)
- varrunsNums=CFArrayGetCount(runs)
- varrw=0.0
- varrh=0.0
- varrowx=0.0
- varrowy=0.0
- ifrunsNums>0
- //将COpaquePointer转为CTRun
- varrun:CTRun=reinterpretCast(CFArrayGetValueAtIndex(runs,0))
- //println("run=\(run)")
- varrunrange=CFRange(location:0,length:(posclm==0)?1:posclm)
- //println("runrange=\(runrange.length)")
- varrunsrect=CTRunGetImageBounds(run,ctx,runrange)
- //println("runsrect=\(runsrect)")
- rw=min(runsrect.size.width,rectWidth-40)
- rh=runsrect.size.height
- rowx=runsrect.origin.x
- rowy=runsrect.origin.y
- varnewline=CTLineCreateTruncatedLine(onlycline,rw,CTLineTruncationType.End,token)
- CTLineDraw(newline,ctx)
- varlinerect=CGRectMake(rowx,rowy,153); background-color:inherit; font-weight:bold">ifnewline
- linerect=CTLineGetImageBounds(newline,ctx)
- }
- //println("本行文字占的rect\(linerect)")
- //rh=min(rh,16)
- //rh=max(rh,10)
- rh=16.0
- moreBtnRect=CGRectMake(linerect.origin.x,
- linerect.origin.y-rh+3,40,rh)
- self.drawMoreInRect(moreBtnRect!)
- varmoretextrect=CGRectMake(moreBtnRect!.origin.x+rh/2+3,moreBtnRect!.origin.y+2,
- moreBtnRect!.size.width-rh,moreBtnRect!.size.height)
- varmoretext:NSString="更多"
- moretext.drawInRect(moretextrect,withFont:UIFont.systemFontOfSize(10))
- break
- CTLineDraw(onlycline,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> textOffset+=fontLineHeight
- }//endfor
- CGContextRestoreGState(ctx);
- varcontentRect=CGRectMake(self.frame.origin.x,self.frame.origin.y,self.bounds.size.width,round(textOffset-self.font.ascender))
- //println("contentRect=\(contentRect)")
- ifcontentRect.size.height>self.bounds.size.height
- dispatch_async(dispatch_get_main_queue(),{
- self.viewFrameChangeBlock(self,contentRect)
- })
- //endif
- }