在CoreText中,很容易问:"对于给定的矩形,这个属性字符串有多少适合?".
CTFrameGetVisibleStringRange(rect).length
Run Code Online (Sandbox Code Playgroud)
将返回字符串中的下一行文本应该开始的位置.
我的问题是:"给定一个属性字符串和宽度,我需要什么矩形高度来完全绑定属性字符串?".
CoreText框架是否提供了执行此操作的工具?
谢谢,
道格
我想用我自己的自定义图形覆盖表情符号图标(仅在我的应用程序中).
从我到目前为止所读到的,一种可能的解决方案是创建一个自定义字体扩展,它会覆盖所需的unicode字符.我希望保持与CATextLayer的互操作性.
编辑:看起来自定义字体不是我的解决方案; 字体必须以灰度定义.下一个可能性:创建一个自定义CALayer,根据表情符号代码将字符串分成多个段,并手动进行类型设置+图形渲染(即使用核心图形和核心文本)
编辑:还希望在表视图中保持平滑的滚动性能.
在OSX 10.6之前,ATSFontActivateFromFileSpecification/ATSFontActivateFromFileReference可用,可用于从文件加载字体.我在Core Text中找不到类似的东西.
CoreText是否有任何设施可用于选择字体的SmallCaps变体,或者如果字体没有该功能,则可以合成小型大写字母?我在CoreText文档中找不到任何关于小型大写的内容,尽管有处理字体变体/功能的工具.有人做过类似的事吗?
我的核心文本有一个非常奇怪的问题,它有时会随机地,有时可重复地崩溃我的应用程序.我用它来布局并渲染几页.我在后台异步执行此操作以不阻止用户界面.
虽然这一般工作正常,但它有时会崩溃.所有这些崩溃发生在同一条线上:
framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)myText);
Run Code Online (Sandbox Code Playgroud)
实际上,它们似乎也来自框架中的类似点.我知道你不喜欢它,但这里是崩溃日志的负责人:
Thread 8 Crashed:
0 ??? 0x0764f446 typeinfo for FT::data_stream + 6
1 libCGFreetype.A.dylib 0x076048b8 FT::font::copy_table(unsigned int) const + 94
2 libCGFreetype.A.dylib 0x0760b085 (anonymous namespace)::copy_table(void*, unsigned int) + 53
3 CoreText 0x00f9592e TBaseFont::CopyTable(unsigned int) const + 334
4 CoreText 0x00f670f6 TAATMorphTable::TAATMorphTable(TLine&, long, unsigned int) + 110
5 CoreText 0x00f6744c TAATMorphTableMorx::TAATMorphTableMorx(TLine&, long, TGlyphList<TDeletedGlyphIndex>&) + 54
6 CoreText 0x00f53eb5 TShapingEngine::ShapeGlyphs(TLine&, TCharStream const&, CFRange&, TGlyphList<TDeletedGlyphIndex>*) + 215
7 CoreText 0x00f579ce TTypesetter::FinishEncoding(TLine&, signed char, TGlyphList<TDeletedGlyphIndex>*) const + …Run Code Online (Sandbox Code Playgroud) 我正在使用伟大的TTTAttributedLabel(https://github.com/mattt/TTTAttributedLabel)在iOS 5下工作正常.但是在iOS 6下,我收到错误:
-[__NSCFType set]: unrecognized selector sent to instance 0x200020e0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [__NSCFType set]: unrecognized selector sent to instance 0x200020e0'
Run Code Online (Sandbox Code Playgroud)
稍微研究了这个问题后,似乎将设置的消息发送到已经发布的对象.使用调试器,我已经po'd 0x200020e0,它似乎是CTFontRef.
po 0x200020e0
(int) $0 = 536879328 CTFont <name: .HelveticaNeueUI-Bold, size: 20.000000, matrix: 0x0>
CTFontDescriptor <attributes: <CFBasicHash 0x20001900 [0x3c2a4100]>{type = mutable dict, count = 1,
entries =>
1 : <CFString 0x3be2a768 [0x3c2a4100]>{contents = "NSFontNameAttribute"} = <CFString 0x3c292c14 [0x3c2a4100]>{contents = ".HelveticaNeueUI-Bold"}
Run Code Online (Sandbox Code Playgroud)
}
这让我直接找到了设置TTTAttributedLabel的代码:
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) …Run Code Online (Sandbox Code Playgroud) 我正在尝试很好地显示NSTextView中突出显示的段落.现在,我是通过创建一个背景颜色的NSAttributedString来做到这一点的.这是一些简化的代码:
NSDictionary *attributes = @{NSBackgroundColorAttributeName:NSColor.greenColor};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"Here is a single line of text with single spacing" attributes:attributes];
[textView.textStorage setAttributedString:attrString];
Run Code Online (Sandbox Code Playgroud)
这种方法基本上有效,因为它产生突出显示的文本.

不幸的是,当存在多条线时,除了线本身之外,高光覆盖线之间的垂直空间,导致丑陋.

有谁知道在Cocoa中进行这种突出显示的方法?下面的图片基本上就是我要找的东西(忽略白框上的阴影):

我愿意使用CoreText,html或任何必要的东西来使事情看起来更好.
我有一个很长的NSString我想在几页上显示.
但要做到这一点,我需要找出页面上实际适合的文本数量.
[NSString sizeWithFont:...]是不够的,它只会告诉我文本是否适合矩形,如果没有,它会默默地截断字符串,但它不会告诉我它被截断的位置!
我需要知道第一个不适合页面的单词,所以我可以分割字符串并在下一页上绘制它的那一部分.(并重复)
任何想法如何解决这个问题?
到目前为止我唯一的想法就是重复调用sizeWithFont:constrainedToSize:在字符串中我猜测分页符的位置,并分析得到的矩形,但感觉很麻烦和缓慢,我觉得我可能有使其100%准确的其他问题...(因为下降,等等.)
ofc,它必须在公共iOS SDK中可用
回答:
Phew,这是一些毛茸茸的文件.这是我完成的功能作为一个例子,也许它会帮助某人,因为那里没有太多特定于iphone的核心文本示例.
+ (NSArray*) findPageSplits:(NSString*)string size:(CGSize)size font:(UIFont*)font;
{
NSMutableArray* result = [[NSMutableArray alloc] initWithCapacity:32];
CTFontRef fnt = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize,NULL);
CFAttributedStringRef str = CFAttributedStringCreate(kCFAllocatorDefault,
(CFStringRef)string,
(CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:(id)fnt,kCTFontAttributeName,nil]);
CTFramesetterRef fs = CTFramesetterCreateWithAttributedString(str);
CFRange r = {0,0};
CFRange res = {0,0};
NSInteger str_len = [string length];
do {
CTFramesetterSuggestFrameSizeWithConstraints(fs,r, NULL, size, &res);
r.location += res.length;
[result addObject:[NSNumber numberWithInt:res.length]];
} while(r.location < str_len);
// NSLog(@"%@",result);
CFRelease(fs);
CFRelease(str);
CFRelease(fnt);
return result;
}
Run Code Online (Sandbox Code Playgroud)
重要的提示:
您不能将返回的范围或大小与任何UIKit类或字符串绘制函数一起使用!您只能将它与Core Text一起使用,例如创建CTFrame并绘制它.像字距调整这样的细微差别使得无法将Core Text函数与UIKit结合起来. …
我打算使用NSAttributedString来突出显示用户搜索的匹配查询的字符串部分.但是,我找不到iOS的等价物NSBackgroundColorAttributeName- 没有kCTBackgroundColorAttributeName.难道这样的事情存在,类似的方式NSForegroundColorAttributeName变kCTForegroundColorAttributeName?
我有一个UILabel字符串'LA'.我也有一个与其属性分配CATextLayer相同的字符.字距中的字距显着不同于.这是代码.NSAttributedStringstringUILabelCATextLayer
- (void)viewDidLoad
{
[super viewDidLoad];
//
// UILabel
//
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 280, 100)];
label1.text = @"LA";
label1.backgroundColor = [UIColor clearColor];
label1.font = [UIFont fontWithName:@"Futura" size:90.0];
[self.view addSubview:label1];
//
// CATextLayer
//
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 130, 280, 100)];
label2.backgroundColor = [UIColor clearColor];
CATextLayer *textLayer = [[CATextLayer alloc] init];
textLayer.frame = label2.layer.bounds;
textLayer.contentsScale = [[UIScreen mainScreen] scale];
[label2.layer addSublayer:textLayer];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"LA"]; …Run Code Online (Sandbox Code Playgroud) core-text ×10
ios ×6
iphone ×4
cocoa ×2
macos ×2
cocoa-touch ×1
ctfontref ×1
emoji ×1
ios6 ×1
macos-carbon ×1
nsstring ×1
nstextview ×1
objective-c ×1
uikit ×1
xcode ×1