我正在尋找在UIView上繪製邊框文字。如何繪製有邊界的字母?
實現下面的方法:
- (void)drawRect:(CGRect)rect {
//TODO draw bordered text here.
}
如何畫呢?
我的意思是每個字母都是整個文本的邊界。
在此先感謝。
我正在尋找在UIView上繪製邊框文字。如何繪製有邊界的字母?
實現下面的方法:
- (void)drawRect:(CGRect)rect {
//TODO draw bordered text here.
}
如何畫呢?
我的意思是每個字母都是整個文本的邊界。
在此先感謝。
要顯示邊框文本(如果我理解正確的話,你想要什麼)你應該設置文本繪製模式kCGTextFillStroke
(並設置文本繪製參數,如中風適當的值和填充顏色等)
// Choose appropriate text font
CGContextSelectFont(context, [[UIFont boldSystemFontOfSize:24].fontName UTF8String], (int)fontSize, kCGEncodingMacRoman);
// Set text drawing mode
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
// Set appropriate color/line width values
// Here we're set to draw white letters with black border
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
CGContextSetRGBFillColor(context, 1, 1, 1, 1);
CGContextSetLineWidth(context, 1);
// Set this transformations or text will be displayed upside-down
CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(context, xform);
// Display text
CGContextShowTextAtPoint(...);
編輯:由於石英不能與unicode一起工作,要繪製unicode字符串,您需要使用其他API。我設法使用NSAttributedString和OHAttributedLabel
(感謝該自定義控件的this answer)繪製「加邊」的unicode字符串。示例代碼來獲得必需的字符串,在一些視圖控制器:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableAttributedString *s = [[NSMutableAttributedString alloc] initWithString:@"您好世界"];
[s addAttributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:-3.0f] forKey:(NSString*)kCTStrokeWidthAttributeName]
range:NSMakeRange(0, [s length])];
[s addAttributes:[NSDictionary dictionaryWithObject:(id)[UIColor greenColor].CGColor forKey:(NSString*)kCTStrokeColorAttributeName]
range:NSMakeRange(0, [s length])];
[s addAttributes:[NSDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(NSString*)kCTForegroundColorAttributeName]
range:NSMakeRange(0, [s length])];
[s setFont:[UIFont boldSystemFontOfSize:28.0f]];
[s setTextColor:[UIColor redColor]];
OHAttributedLabel *l = [[OHAttributedLabel alloc] initWithFrame:CGRectMake(40.0f, 40.0f, 200.0f, 80.0f)];
l.centerVertically = YES;
[l setAttributedText: s];
[self.view addSubview: l];
[l release];
}
請注意,您需要CoreText.framework鏈接,使這項工作,並且代碼使用OHAttributedLabel
實施
我提供了一些方便的方法試試這樣: 用NSString繪製drawAtPoint:withFont:或drawInRect:withFont:
設置線條連接線和線條線圈。
繪製使用繪圖模式kCGTextStroke邊框顏色的文字,一定要設置線寬更寬一點,
然後利用其內部顏色繪製模式kCGTextFill繪製你的文字。
也許你需要做一些調整才能使其完美。