首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在NSString中用两种不同的字体绘制一个drawRect?

在NSString中用两种不同的字体绘制一个drawRect?
EN

Stack Overflow用户
提问于 2012-05-04 13:16:03
回答 3查看 2.2K关注 0票数 1

我有一个字符串作为实例变量,我想用两个不同的字体部分绘制它。具体来说,它的内容是: xyz(abc)。

ie:我想用普通字体画“xyz()”,用斜体画“abc”。

此字符串是一个属性,视图是泛型的。只有在特定的情况下,它才会显示这样的字符串。

我尝试过NSAttributedString及其可变版本,但在iOS上似乎不完全支持它。

不能得到属性的密钥,我该怎么做呢?

这里有使用NSAttributedString的方法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-04 14:11:42

我会将UIView子类用于使用核心文本绘制并传递给它一个自定义的NSAttributed字符串,

从我的头顶上下来,就像这样:

代码语言:javascript
复制
CustomLabel.h
#import <CoreText/CoreText.h>

@interface CustomLabel : UIView
@property NSAttributedString *attributedText;
@end

CustomLabel.m

@interface SMAttributedTextView ()
{
    @protected
    CTFramesetterRef _framesetter;
}

@implementation SMAttributedTextView

@synthesize attributedString = _attributedString;
    //need dealloc even with ARC method to release framesetter if it still exists
- (void)dealloc{
    if (_framesetter) CFRelease(_framesetter);
}

- (void)setAttributedString:(NSAttributedString *)aString
{
    _attributedString = aString;
    [self setNeedsDisplay];//force redraw
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    //Context Drawing Setup
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.frame.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetShouldSubpixelPositionFonts(context, YES);
    CGContextSetShouldSubpixelQuantizeFonts(context, YES);
    CGContextSetShouldAntialias(context, YES);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, rect);

    CTFramesetterRef framesetter = [self framesetter];
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [_attributedText length]), path, NULL);
    CTFrameDraw(frame, context);
    CFRelease(frame);
    CFRelease(path);

    UIGraphicsPushContext(context);
}

@end

因此,在此之外,您将调用attribtued字符串的setter,它应该重新绘制。还可以将自定义特征应用到NSAttributd字符串的范围中,然后再将其发送到视图。您可以使用regexp或普通字符串搜索来查找它。

i.e

代码语言:javascript
复制
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:@"xyz(abc)"];
NSRange rangeOfABC = NSMakeRange(4,3);
//make style dictionary **see core text docs - I can't remeber off the top of my head sorry
[aString addAttributes:newAttrs range:rangeOfABC];

[customlabel setAttributedString:aString];

在我的非工作机器上可能有点不同抱歉所以不能百分之百地验证,

此外,从内存中可以将斜体特征应用于属性化字符串的当前字体。

代码语言:javascript
复制
NSString *targetString = @"xyc(abc)";
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:targetString];
NSRange entireRange = NSMakeRange(0, (targetString.length -1));
NSDictionary *attDict = [attString attributesAtIndex:entireRange.location effectiveRange:&entireRange];
CTFontRef curFontRef = (__bridge CTFontRef)[attDict objectForKey:@"NSFont"];
CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(curFontRef);
BOOL isItalic = ((traits & kCTFontItalicTrait) == kCTFontItalicTrait);

NSMutableDictionary *newAttrs = [NSMutableDictionary dictionary];
CTFontRef italicRef = CTFontCreateCopyWithSymbolicTraits(curFontRef,
                                                         CTFontGetSize(curFontRef),
                                                         NULL,
                                                         kCTFontItalicTrait,
                                                         kCTFontItalicTrait);

if (italicRef)
{
    newAttrs = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)italicRef, kCTFontAttributeName,nil];
    [attString addAttributes:newAttrs range:NSMakeRange(4,3)];//or whatever range you want
    CFRelease(italicRef);
}

希望能帮上忙。

票数 1
EN

Stack Overflow用户

发布于 2012-05-04 13:49:28

我认为您需要使用两个不同的字符串/标签来实现这一效果。据我所知,仅仅使用标签或其他类似的元素是不可能做到的。

在类似的情况下,我还使用了webview,并使用html/css来显示具有不同样式的文本的部分。

票数 0
EN

Stack Overflow用户

发布于 2012-05-04 13:58:58

您必须提供自己的控件来绘制NSAttributedString,如TTTAttributedLabel

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10449590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档