Swift实战

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

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

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

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

源码:

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


效果


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


核心代码

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

猜你在找的Swift相关文章