ios - How to Rotate Core Text in Swift? -
so have following code rotates cgcontext position text should be:
var nums: int = 0 let onums: [int] = [3,4,5,6,7,8,9,10,11,12,1,2] in 0..<60 { cgcontextsavegstate(ctx) cgcontexttranslatectm(ctx, rect.width / 2, rect.height / 2) cgcontextrotatectm(ctx, cgfloat(6.0 * double(i) * m_pi / 180)) cgcontextsetstrokecolorwithcolor(ctx, uicolor.graycolor().cgcolor) cgcontextmovetopoint(ctx, 72, 0) cgcontextsetlinewidth(ctx, 3.0) cgcontextaddlinetopoint(ctx, 42, 0) drawtext(ctx!, text: getroman(onums[nums]), attributes: [nsforegroundcolorattributename : uicolor.blackcolor().cgcolor, nsfontattributename : uifont.systemfontofsize(12)], x: 42, y: 0, align: cgfloat(6.0 * double(i) * m_pi / 180)) nums++ }
where drawtext(_:) is:
func drawtext(context: cgcontextref, text: nsstring, attributes: [string: anyobject], x: cgfloat, y: cgfloat, align: cgfloat) -> cgsize { cgcontexttranslatectm(context, 0, 0) cgcontextscalectm(context, 1, -1) let font = attributes[nsfontattributename] as! uifont let attributedstring = nsattributedstring(string: text string, attributes: attributes) let textsize = text.sizewithattributes(attributes) let textpath = cgpathcreatewithrect(cgrect(x: x, y: y + font.descender, width: ceil(textsize.width), height: ceil(textsize.height)), nil) let framesetter = ctframesettercreatewithattributedstring(attributedstring) let frame = ctframesettercreateframe(framesetter, cfrange(location: 0, length: attributedstring.length), textpath, nil) ctframedraw(frame, context) return textsize }
and getroman(_:) is:
func getroman(numeral: int) -> string {switch numeral {case 1:return "i"case 2:return "ii"case 3:return "iii"case 4:return "iv"case 5:return "v"case 6:return "vi"case 7:return "vii"case 8:return "viii"case 9:return "ix"case 10:return "x"case 11:return "xi"case 12:return "xii"default:return ""}}
the issue, prints text rotated angle (the desired result text angled same degree (standard text orientation))
is there way rotate core text?
regards, brandon
Comments
Post a Comment