如何CGRect从NSRange使用Core Text呈现的文本中获取?
我正在使用Core Text NSAttributedString.
我使用OpenType(.otf)格式的自定义字体,并希望使用该字体的一些OpenType功能.
如何使用UIKit或CoreText实现这一目标?我显然更喜欢UIKit,但是看看UIFont,选项非常有限.
似乎完全没有关于iOS上OpenType支持的文档,除了可以使用字体格式.
相关阅读:Microsoft 对OpenType功能的参考,以及有关浏览器如何开始提供OpenType功能支持的一些信息.虽然这个问题是在iOS上本地渲染具有OpenType功能的字体.
我想在iOS的形状的中心绘制文本,一个例子是微软办公的刀片形状看:https://www.dropbox.com/s/cgqyyuvy6hcv5g8/Screenshot%202014-01-21%2013.48.17巴纽
我有一个用于形成形状的坐标数组,可以很好地创建实际形状.
我的第一个策略是关注这个stackoverflow问题:在iOS上使用非对称形状的核心文本然而我只能在形状的底部绘制文本.有没有办法垂直和水平居中文本?
然后我看了一下新的TextKit,但是我不知道如何将NSTextContainer子类化为接受CGPath或坐标数组来创建要在其中绘制的文本容器.
我的备份解决方案是使用Core Text在形状的中心坐标处绘制文本,但这会导致文本在某些形状(如直角三角形)上绘制到形状之外.
或者,有没有其他方法可以用来在形状的中心稍微获得一些文字?
谢谢,
丹妮尔.
我正在寻找一种方法来获得角色的字形下降,如图所示:
该方法需要适用于任何给定字符(或至少所有常见的unicode字符).
这是我的实际方法(在Swift中,灵感来自于这个和这个问题):
let char = "a"
let ctFont = CTFontCreateWithNameAndOptions("HelveticaNeue", 12, nil, nil)
var ctGlyph = CTFontGetGlyphWithName(ctFont, char)
let boundingBox = withUnsafePointer(&ctGlyph) { pointer -> CGRect in
return CTFontGetBoundingRectsForGlyphs(ctFont, CTFontOrientation.OrientationDefault, pointer, nil, 1)
}
Run Code Online (Sandbox Code Playgroud)
我需要的下降就等于-boundingBox.origin.y.这种方法适用于字母,数字和数字字符(请参阅此答案以获取图形表示).
问题是,对于除字母或数字之外的所有内容(例如.,#')*:),我得到相同的边界矩形:{x:0.612, y:0.012, w:4.908, h:8.532}.这显然是不正确的.
如何直接为所有字符获取下降的粘合矩形?
CTFontRef 提供了很好的方法,例如CTFontGetGlyphsForCharacters将字符映射到字形。我的问题是,是否有任何反转映射的方法?也就是说,我可以通过给定的字形获得字符吗?由于我发现有一个CTFontCopyCharacterSet用于获取所有支持的字符,我认为会有一些不错的解决方案。
我有一个UIView使用CoreText渲染一些文本,一切正常,除了整个视图有黑色背景颜色的事实.我尝试过最基本的解决方案
[self setBackgroundColor:[UIColor clearColor]];
Run Code Online (Sandbox Code Playgroud)
要么
CGFloat components[] = {0.f, 0.f, 0.f, 0.f };
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef transparent = CGColorCreate(rgbColorSpace, components);
CGContextSetFillColorWithColor(context, transparent);
CFRelease(transparent);
Run Code Online (Sandbox Code Playgroud)
但这根本没有帮助.
我只是在这个问题上喋喋不休,因为我不太明白真正的核心文本是如何工作的,这个纯C层如何与所有其他Objective-c相关.这对解决这个问题没什么帮助,所以我问你们大家,你们能不能给我一个解决方案,而且(最重要的是)对它有一个解释.
FIY我也发布了CoreText视图的代码.
这一切都基于两个功能:parseText它建立了CFAttributedString和
drawRect中,是以绘图部分的护理.
-(void) parseText {
NSLog(@"rendering this text: %@", symbolHTML);
attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef)symbolHTML);
CFStringRef fontName = (CFStringRef)@"MetaSerif-Book-Accent";
CTFontRef myFont = CTFontCreateWithName((CFStringRef)fontName, 18.f, NULL);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = { 1.0, 0.3f, 0.3f, 1.f };
CGColorRef redd = CGColorCreate(rgbColorSpace, components);
CFAttributedStringSetAttribute(attrString, CFRangeMake(0,0), …Run Code Online (Sandbox Code Playgroud) 应用kCTParagraphStyleSpecifierParagraphSpacing样式时,它对渲染文本没有视觉效果.其他属性(如行间距和文本对齐)完美地工作.我能做错什么?
CTTextAlignment theAlignment = kCTRightTextAlignment;
CGFloat paragraphSpacingFloat = 150.0;
CGFloat paragraphSpacingBeforeFloat = 150.0;
CGFloat lineSpacing = CTFontGetLeading(baseFont)*5.0;
CFIndex theNumberOfSettings = 4;
CTParagraphStyleSetting theSettings[4] = {
{ kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), ¶graphSpacingFloat },
{ kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), ¶graphSpacingBeforeFloat },
{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &theAlignment },
{ kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &lineSpacing }
};
CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, theNumberOfSettings);
[attr addAttribute:(id)kCTParagraphStyleAttributeName value:(id)theParagraphRef range:r];
[attr addAttribute:(id)kCTFontAttributeName value:(id)baseFont range:r];
CFRelease(theParagraphRef);
Run Code Online (Sandbox Code Playgroud)
我使用渲染文本
CTFrameSetter frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attr);
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake([[attr string] length], 0), the_drawing_cgrect, NULL);
CTFrameDraw(frame, context);
Run Code Online (Sandbox Code Playgroud) 在iOS 7中,不赞成使用几个CGContext文本绘图函数来支持Core Text:
Apple有时会建议使用已弃用的函数的特定替代方法(例如"使用x代替"),但在这种情况下,"x"只是"核心文本",并非完全具体.
所以,如果我只是在-drawRect:,我已经CGContextRef准备好了CGContextShowGlyphsAtPoint(),我准备打电话了,我该怎么做呢?什么是最佳实践,什么是最简单的核心文本替换,不需要重构除该调用之外的任何绘图代码(如果存在这样的事情)?
对于该页面上每个已弃用的文本函数,我们最简单地回答了这个问题的答案的奖励积分 - 我最感兴趣的是CGContextShowGlyphsAtPoint(),但这可以作为其他人寻求替代任何这些函数的良好参考.
经过几个博客和论坛后,我没有找到一个合适的解决方案,用于在视图上下文中使用核心文本绘制倾斜/倾斜文本.
所以这是怎么回事.
我有一个视图,它- (void)drawRect:(CGRect)rect被调用来在屏幕上绘制一个字符串(多行或单行文本).
码:
- (void)drawRect:(CGRect)rect
{
NSString *text = @"This is some text being drawn by CoreText!\nAnd some more text on another line!";
//Core Text (Create Attributed String)
UIColor *textColor = [UIColor blackColor];
CGColorRef color = textColor.CGColor;
CTFontRef font = CTFontCreateWithName((CFStringRef) @"HelveticaNeue", 20.0, NULL);
CTTextAlignment theAlignment = kCTTextAlignmentLeft;
CFIndex theNumberOfSettings = 1;
CTParagraphStyleSetting theSettings[1] =
{
{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),
&theAlignment }
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, theNumberOfSettings);
NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
CFBridgingRelease(font), (NSString …Run Code Online (Sandbox Code Playgroud) 默认布局管理器填充没有文本(最后一行除外)的背景颜色(通过 NSAttributedString .backgroundColor 属性指定)。
我已经通过子类化 NSLayoutManager 并覆盖来实现我想要的效果,func drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint)如下所示:
override func drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint) {
guard let textContainer = textContainers.first, let textStorage = textStorage else { fatalError() }
// This just takes the color of the first character assuming the entire container has the same background color.
// To support ranges of different colours, you'll need to draw each glyph separately, querying the attributed string for the
// background color attribute …Run Code Online (Sandbox Code Playgroud)