gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:36:33 24 4
gpt4 key购买 nike

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

即:我想用普通字体绘制“xyz()”部分,用斜体绘制“abc”。

这个字符串是一个属性, View 是通用的。只有在特定情况下才应该显示这样的字符串。

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

无法获取属性的键,我该怎么办?

无论如何要在这里使用 NSAttributedString 吗?

最佳答案

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

在我脑海中浮现出这样的东西:

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

所以除此之外,您还可以为属性字符串调用 setter,它应该重绘。在将它发送到 View 之前,您还可以将自定义特征应用于 NSAttributd 字符串的范围。您可以使用正则表达式或仅使用一般字符串搜索来找到它。

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];

在代码方面它可能略有不同 - 我在我的非工作机器上抱歉所以不能 100% 验证它,

同样根据内存,这就是如何将斜体特征应用于属性字符串的当前字体

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);
}

希望对您有所帮助。

关于ios - 在 drawRect 中用两种不同的字体绘制一个 NSString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10449590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com