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. 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. 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. letmfont=self.font
  29. varattributes=NSMutableDictionary()
  30. attributes.setObject(mfont,forKey:kCTFontAttributeName)
  31. attributedString.addAttributes(attributes,range:NSMakeRange(0,len))
  32. self.attributedText=attributedString;
  33. varframesetter=CTFramesetterCreateWithAttributedString(attributedString)
  34. varPath=CGPathCreateMutable()
  35. varrectWidth=self.bounds.size.width
  36. CGPathAddRect(Path,nil,CGRectMake(0,rectWidth,CGFLOAT_MAX))
  37. varframe=CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0),Path,nil)
  38. varrows=CTFrameGetLines(frame)
  39. iflethasRows=rows?
  40. //获取frame中的行数
  41. varnumberOfLines=CFArrayGetCount(rows)
  42. //println("rows=\(numberOfLines)")
  43. varfontLineHeight=self.font.lineHeight
  44. vartextOffset=self.font.ascender
  45. varctx=UIGraphicsGetCurrentContext()
  46. CGContextSaveGState(ctx)
  47. //距左上位置设置
  48. CGContextTranslateCTM(ctx,0)
  49. CGContextSetTextMatrix(ctx,CGAffineTransformMakeScale(1,-1))
  50. forvarlineNumber=0;lineNumber<numberOfLines;lineNumber++
  51. varonlyline=CFArrayGetValueAtIndex(rows,lineNumber)
  52. varflush:Double=0.0
  53. varonlycline:CTLine=reinterpretCast(onlyline)
  54. varpenOffset=CTLineGetPenOffsetForFlush(onlycline,flush,rect.size.width)
  55. CGContextSetTextPosition(ctx,penOffset,textOffset);
  56. //println("第\(lineNumber)行\(onlycline)")
  57. //获取第onlycline行中的字形数
  58. //varnums=CTLineGetGlyphCount(onlycline)
  59. //println("本行的字符数\(nums)")
  60. ifletmp=morepoint
  61. varposline=mp.row
  62. varposclm=mp.column
  63. varbool_lastline=(posline==numberOfLines-1)?true:false
  64. iflineNumber==posline&&!self.expand
  65. vartruncatedString=NSAttributedString(string:"\u2026")
  66. vartoken=CTLineCreateWithAttributedString(truncatedString)
  67. varrange=CTLineGetStringRange(onlycline)
  68. varmaxlengthInline=range.length
  69. posclm=min(posclm,maxlengthInline)
  70. //获取onlycline中一黄有几个CTRun,一般情况下,一行中只有一个CTRun
  71. varruns=CTLineGetGlyphRuns(onlycline)
  72. varrunsNums=CFArrayGetCount(runs)
  73. varrw=0.0
  74. varrh=0.0
  75. varrowx=0.0
  76. varrowy=0.0
  77. ifrunsNums>0
  78. //将COpaquePointer转为CTRun
  79. varrun:CTRun=reinterpretCast(CFArrayGetValueAtIndex(runs,0))
  80. //println("run=\(run)")
  81. varrunrange=CFRange(location:0,length:(posclm==0)?1:posclm)
  82. //println("runrange=\(runrange.length)")
  83. varrunsrect=CTRunGetImageBounds(run,ctx,runrange)
  84. //println("runsrect=\(runsrect)")
  85. rw=min(runsrect.size.width,rectWidth-40)
  86. rh=runsrect.size.height
  87. rowx=runsrect.origin.x
  88. rowy=runsrect.origin.y
  89. varnewline=CTLineCreateTruncatedLine(onlycline,rw,CTLineTruncationType.End,token)
  90. CTLineDraw(newline,ctx)
  91. varlinerect=CGRectMake(rowx,rowy,153); background-color:inherit; font-weight:bold">ifnewline
  92. linerect=CTLineGetImageBounds(newline,ctx)
  93. }
  94. //println("本行文字占的rect\(linerect)")
  95. //rh=min(rh,16)
  96. //rh=max(rh,10)
  97. rh=16.0
  98. moreBtnRect=CGRectMake(linerect.origin.x,
  99. linerect.origin.y-rh+3,40,rh)
  100. self.drawMoreInRect(moreBtnRect!)
  101. varmoretextrect=CGRectMake(moreBtnRect!.origin.x+rh/2+3,moreBtnRect!.origin.y+2,
  102. moreBtnRect!.size.width-rh,moreBtnRect!.size.height)
  103. varmoretext:NSString="更多"
  104. moretext.drawInRect(moretextrect,withFont:UIFont.systemFontOfSize(10))
  105. break
  106. CTLineDraw(onlycline,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> textOffset+=fontLineHeight
  107. }//endfor
  108. CGContextRestoreGState(ctx);
  109. varcontentRect=CGRectMake(self.frame.origin.x,self.frame.origin.y,self.bounds.size.width,round(textOffset-self.font.ascender))
  110. //println("contentRect=\(contentRect)")
  111. ifcontentRect.size.height>self.bounds.size.height
  112. dispatch_async(dispatch_get_main_queue(),{
  113. self.viewFrameChangeBlock(self,contentRect)
  114. })
  115. //endif
  116. }

猜你在找的Swift相关文章