Ana*_*and 5 nsmutablestring core-text ios
使用NSAttributedString,我们可以更改字符串等的颜色.是否可以在字符串中倾斜特定字符.例如
答案将不胜感激.
我找到了一个不符合标准的解决方案.如果有人纠正了有用的代码
- (void)drawRect:(CGRect)rect {
CTFontRef sysUIFont = CTFontCreateUIFontForLanguage(kCTFontSystemFontType,
14.0, NULL);
// blue
CGColorRef color = [UIColor blueColor].CGColor;
// single underline
//NSNumber *underline = [NSNumber numberWithInt:kCTUnderlineStyleSingle];
NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)sysUIFont, (id)kCTFontAttributeName,
color, (id)kCTForegroundColorAttributeName, nil];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
for (int i =0 ; i < 4; i++)
{
NSString *letter =[@"TEXT" substringWithRange:NSMakeRange(i, 1)];
NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:letter attributes:attributesDict];
// CGSize letterSize =[letter sizeWithAttributes:@{NSFontAttributeName:self.font}];
// draw
CTLineRef line = CTLineCreateWithAttributedString(
(CFAttributedStringRef)stringToDraw);
if(i==3){
CGContextRotateCTM (context, radians(90));
//18 & 17 are hard coded values
CGContextSetTextPosition(context, (10.0 * i)- 18, 10.0 - (17*i));
}
else{
CGContextSetTextPosition(context, 10.0 * i, 10.0);
}
CTLineDraw(line, context);
// clean up
CFRelease(line);
stringToDraw = nil;
}
CFRelease(sysUIFont);
//stringToDraw = nil;
}
Run Code Online (Sandbox Code Playgroud)
OutPut:
NSAffineTransform类提供了创建、连接和应用仿射变换的方法。
\n\nrotateByDegrees
:\n将旋转因子(以度为单位)应用于接收器\xe2\x80\x99s 变换矩阵。
rotateByRadians
: \n将旋转因子(以弧度测量)应用于接收器\xe2\x80\x99s 变换矩阵。
例子:
\n\n- (void)drawRect:(NSRect)rect\n{\n NSAttributedString * myString = [[NSAttributedString alloc] initWithPath:mypath documentAttributes:NULL];\n NSAffineTransform* xform = [NSAffineTransform transform];\n [myString drawAtPoint:NSMakePoint(10,10)];\n [xform rotateByDegrees:-90.0];\n [xform concat];\n [myString drawAtPoint:NSMakePoint(-150,0)];\n [xform invert];\n [xform concat];\n [myString drawAtPoint:NSMakePoint(15,15)];\n [myString release];\n}\n
Run Code Online (Sandbox Code Playgroud)\n