我正在尝试研究如何获取NSAttributedString并在iPad上的Core Text中使用它.我观看了一个WWDC视频(110),其中有幻灯片(但没有源代码),它描述了如何创建NSAttributedString,然后将其放入CTFramesetterRef:
CTFontRef helveticaBold = CTFontCreateWithName( CFSTR("Helvetica-Bold"), 24.0, NULL);
NSString* unicodeString = NSLocalizedString(@"TitleString", @"Window Title");
CGColorRef color = [UIColor blueColor].CGColor; NSNumber* underline = [NSNumber numberWithInt:kCTUnderlineStyleSingle|kCTUnderlinePatternDot];
NSDictionary* attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:helveticaBold, (NSString*)kCTFontAttributeName, color, (NSString*)kCTForegroundColorAttributeName, underline, (NSString*)kCTUnderlineStyleAttributeName, nil];
NSAttributedString* stringToDraw = [[NSAttributedString alloc] initWithString:unicodeString attributes:attributesDict];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(stringToDraw);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CFRelease(framesetter);
CTFrameDraw(frame, context);
CFRelease(frame);
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这个时,它会抛出错误:
从不兼容的指针类型传递'CTFramesetterCreateWithAttributedString'的参数1
CFAttributedString和NSAttributedString'免费桥接'?我已经在某处读过,这只适用于OSX,但这就是Apple在他们的演示中如何做到这一点......
谢谢!
:-Joe
我正在尝试将一些代码移植到使用UIFont和的Swift CTFont,并且(成功地,在Objective-C中)使用简单的桥接转换来从一个转换到另一个,反之亦然.
例如,考虑此代码(在UIFontDescriptor类别中):
UIFont *font = [UIFont fontWithDescriptor:self size:0.0];
NSArray *features = CFBridgingRelease(CTFontCopyFeatures((__bridge CTFontRef)font));
Run Code Online (Sandbox Code Playgroud)
我还没有弄清楚如何以一种实际编译的方式在Swift中表达这一点.以下至少不会:
let font = UIFont(descriptor:self, size: 0.0)
let features = CTFontCopyFeatures(font as CTFont)
Run Code Online (Sandbox Code Playgroud)
错误:'UIFont'无法转换为'CTFont'
我在视图中有一些代码,它使用CoreText绘制一些属性文本.在这里,我正在搜索网址并将其设为蓝色.我们的想法是不要UIWebView为了获得可点击链接而带来所有开销.一旦用户点击该链接(而不是整个表视图单元格),我想触发一个委托方法,然后该方法将用于呈现一个模态视图,其中包含一个转到该URL的Web视图.
我将路径和字符串本身保存为视图的实例变量,并且绘图代码发生在-drawRect:(我为了简洁而将其遗漏).
然而,我的触摸处理程序虽然不完整,却没有打印出我期望它的内容.它在下面:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGContextRef context = UIGraphicsGetCurrentContext();
NSLog(@"attribString = %@", self.attribString);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.attribString);
CTFrameRef ctframe = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, self.attribString.length), attribPath, NULL);
CFArrayRef lines = CTFrameGetLines(ctframe);
for(CFIndex i = 0; i < CFArrayGetCount(lines); i++)
{
CTLineRef line = CFArrayGetValueAtIndex(lines, i);
CGRect lineBounds = CTLineGetImageBounds(line, context);
// Check if tap was on our line
if(CGRectContainsPoint(lineBounds, point))
{
NSLog(@"Tapped line");
CFArrayRef …Run Code Online (Sandbox Code Playgroud) 我可以使用Core Text绘制富文本,问题是将图像与文本一起流动.(iOS SDK 4.1)
我试着绘制某种富文本.问题是设计师在文本中放置了许多图标.所以我要绘制的文字是这样的:
Run Code Online (Sandbox Code Playgroud)Here is a word <an icon image>, and another words. The image(<another icon>) should be placed like a glyph. It's part of text, not an example.
<icon>是图像.(这不是代码.只是一个例子.)
我可以通过手动布局所有这些来绘制它,但是保持复杂的文本布局行为太难了.所以我找到了一种用Core Text绘制它的方法.
我正在使用应用于CATextLayer的自定义字体.我正在使用此处的教程和示例代码.http://www.freetimestudios.com/2010/09/13/custom-fonts-on-the-ipad-and-ios-4/
但是,每隔一段时间,我就会收到以下错误.没有什么特别可以触发它.有人可以说明这意味着什么吗?要检查什么?
CoreText: Invalid 'kern' Subtable In CTFont <name: Intellect, size: 20.000000, matrix: 0x0>
CTFontDescriptor <attributes: <CFDictionary 0xac07a80 [0x38295d98]>{type = mutable, count = 1, capacity = 3, pairs = (
0 : <CFString 0x3833f750 [0x38295d98]>{contents = "NSFontNameAttribute"} = <CFString 0x159af0 [0x38295d98]>{contents = "Intellect"}
Run Code Online (Sandbox Code Playgroud)
我使用以下代码加载字体.此代码取自上面链接中的项目.
- (CTFontRef)newCustomFontWithName:(NSString *)fontName
ofType:(NSString *)type
attributes:(NSDictionary *)attributes
{
NSString *fontPath = [[NSBundle mainBundle] pathForResource:fontName ofType:type];
NSData *data = [[NSData alloc] initWithContentsOfFile:fontPath];
CGDataProviderRef fontProvider = CGDataProviderCreateWithCFData((CFDataRef)data);
[data release];
CGFontRef cgFont = CGFontCreateWithDataProvider(fontProvider);
CGDataProviderRelease(fontProvider);
CTFontDescriptorRef fontDescriptor = …Run Code Online (Sandbox Code Playgroud) 我正在尝试添加一个超链接到我正在使用CoreText处理和绘制的文本的某些部分.
根据Apple在CoreText上的文档,我应该addAttribute:NSLinkAttributeName在iOS(4.3)上使用它,它说它不知道NSLinkAttributeName.在我的文档搜索中,看起来只有NSLinkAttributeName仍然存在于Mac上.
它是否可在iOS上使用,我只是遗漏了一些东西?如果它不可用,我如何使用NSMutableAttributedString和CoreText在文本的一部分上创建超链接?
谢谢
属性字符串只有一个属性 - 17点Helvetica NeueUI字体 - 覆盖整个字符串.1~3行纯英语,4~6行是英汉混合,7~8行纯粹是中文.
然后使用CTFramesetter进行布局,并使用CTFrameDraw绘制结果框架.
// UIView subclass
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica NeueUI"), 17.f, NULL);
_text = [[NSAttributedString alloc] initWithString:string attributes:
[NSDictionary dictionaryWithObject:(id)font forKey:(id)kCTFontAttributeName]];
CFRelease(font);
_framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)_text);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds);
_frame = CTFramesetterCreateFrame(_framesetter, CFRangeMake(0, 0), path, NULL);
CGPathRelease(path);
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
// Flip the coordinate system.
CGContextTranslateCTM(context, 0.f, self.bounds.size.height);
CGContextScaleCTM(context, …Run Code Online (Sandbox Code Playgroud) 我有一个自定义子类,UILabel使用CoreText进行自定义绘图.我曾经使用在UIFont使用font属性访问它的标签上设置的字符串来绘制我的字符串.我还在字体中添加了一些特征.这是它的样子:
UIFont* font = [self font];
CTFontRef ref = CTFontCreateWithName((CFStringRef)[font name], [font pointSize], NULL);
CTFontRef italicFont = CTFontCreateCopyWithSymbolicTraits(ref, [font pointSize], NULL, kCTFontItalicTrait, kCTFontItalicTrait);
Run Code Online (Sandbox Code Playgroud)
这曾经很好地工作,直到新的ipad出现其视网膜显示器.当我执行CTFontCreateCopyWithSymbolicTraits调用时,它会产生一个nil返回值.我发现[font name]在这些新设备上返回".HelveticaNeueUI".在CTFontCreateWithName似乎好工作与这家民营字体的名字,但CTFontCreateCopyWithSymbolicTraits没有.然而,如果我使用[UIFont italicSystemFontOfSize:[font pointSize]]它创建一个斜体字体,则会创建一个[font name]仍然返回".HelveticaNeueUI" 的斜体字体.
我应该如何将我转换UIFont为CTFontRef能够CTFontRef在视网膜和非视网膜显示器上应用新的特征?
这个问题是关于在iOS中实现文本缩进("将文本放在更远的位置以将其与周围文本分开").
以下面的文字为例:
请注意,第2部分中的第二行从右侧开始,在上方的线下方.
我的代码包含一个数组NSString,每个应该显示为带有上面数字项目符号的部分.例如:
NSArray *array = [NSArray arrayWithObjects:@"1. This is the first section.", @"2. This is the second one, with two lines.", @"3. This is the third.", nil];
Run Code Online (Sandbox Code Playgroud)
我用来UILable在屏幕上显示文字.
要将文本从数组设置为标签,并将我使用的新行中的每个字符串分开
myLabel.text = [array componentsJoinedByString:@"\n"];
Run Code Online (Sandbox Code Playgroud)
任何想法如何获得这种效果?
我一直在研究一个大项目(无论如何都没有显示任何实际代码)我注意到日志中出现以下消息:
CoreText CopyFontsForRequest received mig IPC error (FFFFFFFFFFFFFECC) from font server
Run Code Online (Sandbox Code Playgroud)
WebView完成加载后会立即弹出错误.我有点相信这是一个小小的滞后背后的罪魁祸首.
为什么会这样?我该怎么做才能解决这个问题?
PS尝试在这里建议的解决方案,以检查它是否是系统特定的,但它不起作用.
更多细节:
从Appearance Maker项目使用AMEditorAppearance.car NSAppearance文件时出现错误.禁用它(=不加载全部)会使错误消失.
我并不真正关心错误消息,除了它会产生一些奇怪的字体问题.例如NSAlert,带有输入fiels的面板显示出明显的闪烁,字体/文本似乎相当混乱,我不确定我能准确描述的方式.(如果有帮助,我可以发布一个视频)