这是在UITextView中获取文本的CGRect的延续,目的是使用CALayer进行突出显示.我在为每个线段中的范围获取正确的矩形时遇到问题.
NSString* searchString = @"Returns the range of characters that generated";
NSRange match = [[[self textView]text]rangeOfString:searchString];
NSRange matchingGlyphRange = [manager glyphRangeForCharacterRange:match actualCharacterRange:NULL];
[manager enumerateLineFragmentsForGlyphRange:matchingGlyphRange usingBlock:
^(CGRect lineRect, CGRect usedRect, NSTextContainer *textContainer, NSRange lineRange, BOOL *stop) {
NSRange currentRange = NSIntersectionRange(lineRange, matchingGlyphRange);
[manager enumerateEnclosingRectsForGlyphRange:currentRange withinSelectedGlyphRange:NSMakeRange(NSNotFound, 0) inTextContainer:textContainer usingBlock:
^(CGRect rect, BOOL* stop) {
if (usedRect.origin.y == rect.origin.y && NSLocationInRange(currentRange.location, lineRange)) {
CGRect theRect = [manager boundingRectForGlyphRange:currentRange inTextContainer:textContainer];
CALayer* roundRect = [CALayer layer];
[roundRect setFrame:theRect];
[roundRect …Run Code Online (Sandbox Code Playgroud) 我有一个NSAttributedString报告一个boundingRectWithSize(和扩展名为UITextView,它不正确地计算其sizeThatFits)当字体大小从用于创建它的字体大小减少时.
在我执行类似操作的所有NSAttributedStrings上都不会发生这种情况,因此这里是重现的步骤.
从UITextView子类内部:
NSMutableAttributedString *mutableString = [self.attributedText mutableCopy];
[mutableString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, mutableString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
UIFont *oldFont = (UIFont *)value;
UIFont *newFont = [oldFont fontWithSize:oldFont.pointSize - 1];
[mutableString removeAttribute:NSFontAttributeName range:range];
[mutableString addAttribute:NSFontAttributeName value:newFont range:range];
}
}];
self.attributedText = [mutableString copy];
Run Code Online (Sandbox Code Playgroud)
我注意到,在while循环中运行此代码时,检查sizeThatFits以了解文本何时足够小以适应,我会在某些情况下发生竞争.对于任何小于我开始时的字体值,高度计算为60px,恰好是50px.
在NSLog使用NSAttributedString时,我发现有几个属性我没有使用该键添加,而这些属性NSOriginalFont似乎不在此处支持的属性列表中.NSOriginalFont发生了什么?为什么我的尺寸计算不正确?
嗨,我正在使用TextKitiOS7 的新API,我正在尝试生成一个UITextView不规则的形状.到目前为止,我在视图控制器中:
-(void) loadView
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,548)];
NSTextStorage *textStorage = [[NSTextStorage alloc] init];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager: layoutManager];
BaseTextContainer *textContainer = [[BaseTextContainer alloc] initWithSize:CGSizeMake(100, 100)];
[layoutManager addTextContainer: textContainer];
BaseTextView *textView = [[BaseTextView alloc] initWithFrame:CGRectMake(110,124, 100, 100) textContainer:textContainer];
textView.backgroundColor = [UIColor blueColor];
textView.editable = YES;
[self.view addSubview:textView];
}
Run Code Online (Sandbox Code Playgroud)
然后在我的子类中NSTextContainer,我想mutablePath绘制一个文本容器的形状,但不知道如何实现这一点.我有:
- (BOOL) isSimpleRectangularTextContainer
{
return NO;
}
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
NSLog(@"TEST");
CGContextRef context = …Run Code Online (Sandbox Code Playgroud) 我有一个自定义NSLayoutManager子类,用于绘制药丸状标记。我为带有自定义属性(TokenAttribute)的子字符串绘制了这些标记。我没问题。
但是,我需要在范围周围添加一点“填充” TokenAttribute(这样标记的圆角矩形背景将不会与文本相交)。
在上图中,我正在用橙色绘制令牌的背景,但我想在周围加些填充,469以使背景不紧贴文本。
我不太确定该怎么做。我尝试重写-boundingRectForGlyphRange:inTextContainer:以返回具有更多水平填充的边界矩形,但字形的布局实际上并不受此影响。
如何在某些字形/字形范围内增加间距?
这是我用于在布局管理器子类中绘制背景的代码:
- (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin {
NSTextStorage *textStorage = self.textStorage;
NSRange glyphRange = glyphsToShow;
while (glyphRange.length > 0) {
NSRange characterRange = [self characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
NSRange attributeCharacterRange;
NSRange attributeGlyphRange;
id attribute = [textStorage attribute:LAYScrubbableParameterAttributeName
atIndex:characterRange.location
longestEffectiveRange:&attributeCharacterRange
inRange:characterRange];
attributeGlyphRange = [self glyphRangeForCharacterRange:attributeCharacterRange
actualCharacterRange:NULL];
attributeGlyphRange = NSIntersectionRange(attributeGlyphRange, glyphRange);
if (attribute != nil) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
UIColor *backgroundColor = [UIColor orangeColor];
NSTextContainer *textContainer = self.textContainers[0];
CGRect …Run Code Online (Sandbox Code Playgroud) 我正在使用 Markdown 语法的 Mac 应用程序中的富文本编辑器。我NSTextStorage过去常常在 Markdown 语法中观察匹配项,然后NSAttributedString像这样实时地将样式应用到:
在这一点上,我已经在这方面不知所措,但我很高兴能够取得进展。:)本教程非常有帮助。
作为下一步,我想在呈现 's 字符串时隐藏 Markdown 字符NSTextView。所以在上面的例子中,一旦最后一个星号被输入,我希望* *字符被隐藏,只是sample以粗体显示。
我正在使用NSLayoutManager委托,我可以看到匹配的字符串,但我不清楚如何使用该shouldGenerateGlyphs方法生成修改后的字形/属性。这是我到目前为止所拥有的:
func layoutManager(_: NSLayoutManager, shouldGenerateGlyphs _: UnsafePointer<CGGlyph>, properties _: UnsafePointer<NSLayoutManager.GlyphProperty>, characterIndexes _: UnsafePointer<Int>, font _: NSFont, forGlyphRange glyphRange: NSRange) -> Int {
let pattern = "(\\*\\w+(\\s\\w+)*\\*)" // Look for stuff like *this*
do {
let regex = try NSRegularExpression(pattern: pattern)
regex.enumerateMatches(in: textView.string, range: glyphRange) {
match, _, _ in …Run Code Online (Sandbox Code Playgroud) 我的目标是使用TextKit来斜体,设置某些单词的文本大小等.
首先,我只想在文本字符串中突出显示一个字符.作为TextKit的新手(以及一般的编程),我遵循obc.io问题#5的语法高亮主题.
当我使用内置于我创建的UITextView的NSLayoutManager时,我的文本出现在屏幕上,没有抛出异常.当我在我的视图控制器(下面)中将我的UITextView设置为我的NSTextStorage子类的布局管理器时,我收到了无效字形索引的异常错误.
_textStorage = [BBRSyntaxHighlightTextStorage new];
[_textStorage addLayoutManager: self.readerTextView.layoutManager];
Run Code Online (Sandbox Code Playgroud)
控制台输出如下:
_NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 528
2013-12-01 15:23:24.949 BibleReader[6077:70b] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1
2013-12-01 15:23:24.956 BibleReader[6077:70b] !!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 528
2013-12-01 15:23:24.957 BibleReader[6077:70b] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1
2013-12-01 15:23:24.957 BibleReader[6077:70b] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange character count mismatch
2013-12-01 15:23:24.958 BibleReader[6077:70b] !!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 4040
2013-12-01 15:23:24.959 BibleReader[6077:70b] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1
Run Code Online (Sandbox Code Playgroud)
我已多次阅读Apple的文本编程指南,并认为我理解文本系统是如何建立的,但不知道为什么我的字形数量会超过字形的数量......
我想在iOS的形状的中心绘制文本,一个例子是微软办公的刀片形状看:https://www.dropbox.com/s/cgqyyuvy6hcv5g8/Screenshot%202014-01-21%2013.48.17巴纽
我有一个用于形成形状的坐标数组,可以很好地创建实际形状.
我的第一个策略是关注这个stackoverflow问题:在iOS上使用非对称形状的核心文本然而我只能在形状的底部绘制文本.有没有办法垂直和水平居中文本?
然后我看了一下新的TextKit,但是我不知道如何将NSTextContainer子类化为接受CGPath或坐标数组来创建要在其中绘制的文本容器.
我的备份解决方案是使用Core Text在形状的中心坐标处绘制文本,但这会导致文本在某些形状(如直角三角形)上绘制到形状之外.
或者,有没有其他方法可以用来在形状的中心稍微获得一些文字?
谢谢,
丹妮尔.
我正在使用ios7中的Text Kit构建一个注释编辑器.早些时候,我遇到了麻烦,在自定义尺寸的渲染NSTextAttachment的AS它呈现放缓的大extent.I解决这个问题通过缩放图像,然后将它们添加到textview.You可以找到我的回答 的iOS 7.0的UITextView gettings后非常慢向图像添加图像 缩放图像后,文本视图渲染运行正常,没有任何滞后.文本视图的属性文本存储在核心数据中.在应用程序运行时,textview正确显示图像.即使在核心中保存属性文本后也是如此数据并再次检索以显示在textview上,图像看起来很好.但是在杀死应用程序并再次运行应用程序之后.图像被放大到2倍大小.在缩放图像时我使用了以下功能并使用了[[UIScreen bounds]比例]以保持图像质量.
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Run Code Online (Sandbox Code Playgroud)
如果我将图像缩放到1.0,图像不会扩展,但图像质量非常差.
我认为问题出在哪里? 问题在于布局管理器.
我曾尝试
我试图子类NSLayoutManager并重写
- (空白)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)原产
我看到的是运行application.If我尝试的一个新的会话时,附件大小加倍检查附件的大小并调整大小.滞后开始再次出现.我很快就遇到了这个问题.任何建议都会非常感激.

我需要开发一个具有多列的文本编辑器(UITextView).我目前正在尝试使用Text Kit来完成此任务.不幸的是,我们只能在UITextView(textContainer属性)中使用一个TextContainer.
我已经看到很多实现渲染文本UITextView有多列我们的要求是不同的,因为它必须是可编辑的.多个UITextViews不起作用,因为我们需要正确调整文本位置和光标从上一列/下一列溢出.
有人有过这个吗?
我想实现中型iOS应用程序,如点击高亮显示效果和显示工具提示.
我一直在研究Text Kit和其他一些stackoverflow问题有一些想法,请也建议什么是更好的替代品.
场景:
解决思路:
UITextView用于存储文本NSBackgroundColorAttributedNamelayoutManager.characterIndexForPoint(...)AMPopTip,CMPopTipView,EasyTipView现在,我无法选择单词并显示旁边的工具提示.任何层次的帮助表示赞赏.
textkit ×10
ios ×8
uitextview ×5
cocoa-touch ×2
ios7 ×2
objective-c ×2
core-text ×1
fonts ×1
nstextview ×1
swift ×1
tooltip ×1