Swift实战

前端之家收集整理的这篇文章主要介绍了Swift实战前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

swift学习也快有一个月了。文章也写了不少。今天来发布一个小DEMO。当操刀练手了。

主要写一个小控件,实现功能是类拟IPHONE手机打开AppStore 中的详细信息中的内容,先显示一部分,点击更多时,再加载完成。

目前只对单文本字体作处理,对于富文本的并不行。因此作为swift的操刀之作,还算是处女作吧。。。。。

源码:

整个控件的源码在:http://download.csdn.net/detail/fengsh998/7552229


效果


其中这个“更多“的位置可以自行调整,当点击更多时,就会展示全部:


核心代码

  1. //morebtn
  2. funcdrawMoreInRect(moreRect:CGRect)
  3. {
  4. varctx=UIGraphicsGetCurrentContext();
  5. CGContextSaveGState(ctx);
  6. varraduis=CGRectGetHeight(moreRect)/2.0
  7. varmaxX=CGRectGetMaxX(moreRect)
  8. varminX=CGRectGetMinX(moreRect)
  9. varmaxY=CGRectGetMaxY(moreRect)
  10. varminY=CGRectGetMinY(moreRect)
  11. CGContextMoveToPoint(ctx,minX,minY)
  12. CGContextAddArcToPoint(ctx,maxX,minY,maxY,raduis)
  13. CGContextAddArcToPoint(ctx,raduis)
  14. @H_502_101@ CGContextClosePath(ctx)
  15. ifselectedHightlight
  16. {
  17. CGContextSetRGBFillColor(ctx,29/255.0,158/255.0,245/255.0,1.0)
  18. }
  19. else
  20. CGContextDrawPath(ctx,kCGPathFill)//根据坐标绘制路径
  21. CGContextRestoreGState(ctx)
  22. overridefuncdrawRect(rect:CGRect)
  23. @H_502_101@ varattributedString=NSMutableAttributedString(string:self.text)
  24. letlen=countElements(String(self.text))
  25. letfname=self.font.fontName
  26. //StringconverttoCFString
  27. //letcfstr:CFString=reinterpretCast(fname.withCString(getenv))
  28. @H_502_101@ letmfont=self.font @H_502_101@ varattributes=NSMutableDictionary() @H_502_101@ attributes.setObject(mfont,forKey:kCTFontAttributeName) @H_502_101@ attributedString.addAttributes(attributes,range:NSMakeRange(0,len)) @H_502_101@ self.attributedText=attributedString; @H_502_101@ varframesetter=CTFramesetterCreateWithAttributedString(attributedString) @H_502_101@ varPath=CGPathCreateMutable() @H_502_101@ varrectWidth=self.bounds.size.width @H_502_101@ CGPathAddRect(Path,nil,CGRectMake(0,rectWidth,CGFLOAT_MAX)) @H_502_101@ varframe=CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0),Path,nil) @H_502_101@ varrows=CTFrameGetLines(frame) @H_502_101@ iflethasRows=rows?
  29. //获取frame中的行数
  30. varnumberOfLines=CFArrayGetCount(rows)
  31. //println("rows=\(numberOfLines)")
  32. @H_502_101@ varfontLineHeight=self.font.lineHeight @H_502_101@ vartextOffset=self.font.ascender @H_502_101@ varctx=UIGraphicsGetCurrentContext()
  33. CGContextSaveGState(ctx)
  34. //距左上位置设置
  35. CGContextTranslateCTM(ctx,0)
  36. CGContextSetTextMatrix(ctx,CGAffineTransformMakeScale(1,-1))
  37. forvarlineNumber=0;lineNumber<numberOfLines;lineNumber++
  38. @H_502_101@ varonlyline=CFArrayGetValueAtIndex(rows,lineNumber)
  39. varflush:Double=0.0
  40. varonlycline:CTLine=reinterpretCast(onlyline)
  41. varpenOffset=CTLineGetPenOffsetForFlush(onlycline,flush,rect.size.width)
  42. CGContextSetTextPosition(ctx,penOffset,textOffset);
  43. //println("第\(lineNumber)行\(onlycline)")
  44. //获取第onlycline行中的字形数
  45. //varnums=CTLineGetGlyphCount(onlycline)
  46. //println("本行的字符数\(nums)")
  47. ifletmp=morepoint
  48. @H_502_101@ varposline=mp.row
  49. varposclm=mp.column
  50. varbool_lastline=(posline==numberOfLines-1)?true:false
  51. iflineNumber==posline&&!self.expand
  52. vartruncatedString=NSAttributedString(string:"\u2026")
  53. vartoken=CTLineCreateWithAttributedString(truncatedString)
  54. @H_502_101@ varrange=CTLineGetStringRange(onlycline)
  55. varmaxlengthInline=range.length
  56. posclm=min(posclm,maxlengthInline)
  57. //获取onlycline中一黄有几个CTRun,一般情况下,一行中只有一个CTRun
  58. varruns=CTLineGetGlyphRuns(onlycline)
  59. varrunsNums=CFArrayGetCount(runs)
  60. varrw=0.0
  61. varrh=0.0
  62. varrowx=0.0
  63. varrowy=0.0
  64. ifrunsNums>0
  65. //将COpaquePointer转为CTRun
  66. varrun:CTRun=reinterpretCast(CFArrayGetValueAtIndex(runs,0))
  67. //println("run=\(run)")
  68. varrunrange=CFRange(location:0,length:(posclm==0)?1:posclm)
  69. //println("runrange=\(runrange.length)")
  70. varrunsrect=CTRunGetImageBounds(run,ctx,runrange)
  71. //println("runsrect=\(runsrect)")
  72. rw=min(runsrect.size.width,rectWidth-40)
  73. rh=runsrect.size.height
  74. rowx=runsrect.origin.x
  75. rowy=runsrect.origin.y
  76. varnewline=CTLineCreateTruncatedLine(onlycline,rw,CTLineTruncationType.End,token)
  77. CTLineDraw(newline,ctx)
  78. varlinerect=CGRectMake(rowx,rowy,153); background-color:inherit; font-weight:bold">ifnewline
  79. @H_502_101@ linerect=CTLineGetImageBounds(newline,ctx)
  80. }
  81. //println("本行文字占的rect\(linerect)")
  82. //rh=min(rh,16)
  83. //rh=max(rh,10)
  84. rh=16.0
  85. @H_502_101@ moreBtnRect=CGRectMake(linerect.origin.x,
  86. linerect.origin.y-rh+3,40,rh)
  87. self.drawMoreInRect(moreBtnRect!)
  88. varmoretextrect=CGRectMake(moreBtnRect!.origin.x+rh/2+3,moreBtnRect!.origin.y+2,
  89. moreBtnRect!.size.width-rh,moreBtnRect!.size.height)
  90. varmoretext:NSString="更多"
  91. moretext.drawInRect(moretextrect,withFont:UIFont.systemFontOfSize(10))
  92. break
  93. @H_502_101@ CTLineDraw(onlycline,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> textOffset+=fontLineHeight
  94. }//endfor
  95. CGContextRestoreGState(ctx);
  96. varcontentRect=CGRectMake(self.frame.origin.x,self.frame.origin.y,self.bounds.size.width,round(textOffset-self.font.ascender))
  97. //println("contentRect=\(contentRect)")
  98. ifcontentRect.size.height>self.bounds.size.height
  99. @H_502_101@ dispatch_async(dispatch_get_main_queue(),{
  100. self.viewFrameChangeBlock(self,contentRect)
  101. })
  102. //endif
  103. }

猜你在找的Swift相关文章