标签: core-text

CoreText中的行间距和段落对齐方式

我正在使用CoreText来呈现多列文本.但是,当我将第一段的第一个字母设置为比文本的其余部分更粗体,更大的字体时,我会产生2个问题(在附加图像中都可见):

  1. 第一行下面的间距太大(我明白这是因为第一个字符可能是ag,y,p,q等.

  2. 第一行下面的行现在不与下一列中的相应行对齐.

如何克服这两个问题的任何建议将不胜感激,谢谢. 在此输入图像描述

iphone cocoa cocoa-touch ipad core-text

5
推荐指数
1
解决办法
4492
查看次数

CATextLayer + NSAttributtedString + CTParagraphStyleRef

我想要一些带有自定义行距的文本,所以我写了一个属性字符串CTParagraphStyleAttributte并将其传递给我的CATextLayer

UIFont *font = [UIFont systemFontOfSize:20];
CTFontRef ctFont = CTFontCreateWithName((CFStringRef)font.fontName,
                                        font.pointSize, NULL);
CGColorRef cgColor = [UIColor whiteColor].CGColor;
CGFloat leading = 25.0;
CTTextAlignment alignment = kCTRightTextAlignment; // just for test purposes
const CTParagraphStyleSetting styleSettings[] = {
    {kCTParagraphStyleSpecifierLineSpacingAdjustment, sizeof(CGFloat), &leading},
    {kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &alignment}
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(styleSettings, 2));
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                            (id)ctFont, (id)kCTFontAttributeName,
                            (id)cgColor, (id)kCTForegroundColorAttributeName,
                            (id)paragraphStyle, (id)kCTParagraphStyleAttributeName,
                            nil];
CFRelease(ctFont);
CFRelease(paragraphStyle);

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] 
                                                 initWithString:string
                                                     attributes:attributes];
_textLayer.string = attrStr;
[attrStr release];
Run Code Online (Sandbox Code Playgroud)

但是行高没有改变。我想我在这里遗漏了一些东西,但我不知道是什么。

我已经尝试过kCTParagraphStyleSpecifierLineSpacingAdjustment …

core-animation objective-c core-text ios catextlayer

5
推荐指数
1
解决办法
4035
查看次数

核心图形文本比核心文本更快 - 我觉得我错过了一些东西

这是我用来在核心图形中显示一些文本的代码:

int y = MESSAGE_FRAME.origin.y + 8;

if (month) y = y + 27;
int height = [JHomeViewCellContentView heightOfMessage:self.entry.message];

CGRect rect = CGRectMake(MESSAGE_FRAME.origin.x + 8, y, MESSAGE_FRAME.size.width - 16, height);

UIFont *font = [UIFont fontWithName:@"Crimson" size:15.0f];

[[UIColor colorWithWhite:0.2 alpha:1.0] setFill];

[self.entry.message drawInRect:rect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
Run Code Online (Sandbox Code Playgroud)

如果我想在核心文本中显示相同的文本,这是代码:

CGRect rect2 = CGRectMake(MESSAGE_FRAME.origin.x + 8, self.bounds.size.height - y - height + 2, MESSAGE_FRAME.size.width - 16, height);


    UIFont *font = [UIFont fontWithName:@"Crimson" size:15.0f];
    CGFloat lineHeight = [font lineHeight];

    CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"Crimson", 15.0f, NULL); …
Run Code Online (Sandbox Code Playgroud)

iphone core-graphics objective-c core-text ios

5
推荐指数
1
解决办法
2257
查看次数

如何绘制在顶部和右下方的对象周围流动的文本?

我有类似这个问题的问题,但更复杂.我正在尝试在表格视图单元格中绘制文本,其中右上角是日期标签,右下角是徽章,如下所示:

+-----------------------+----+
| Lorem ipsum dolor sit |DATE|
| amet, consectetur     +----+
| adipiscing elit. Integer   |
| ligula lectus, convallis   |
| non scelerisque      +-----+
| quis, tempor at nibh |BADGE|
+----------------------+-----+
Run Code Online (Sandbox Code Playgroud)

如果有更多或更少的文本,我希望单元格更大或更小:

+-----------------------+----+
| Lorem ipsum dolor sit |DATE|
| amet, consectetur     +----+
| adipiscing elit. Integer   |
| ligula lectus, convallis   |
| non scelerisque quis,      |
| tempor at nibh.  Curabitur |
| eget diam ligula.          |
| Pellentesque a elit        |
| dolor, …
Run Code Online (Sandbox Code Playgroud)

objective-c uitableview core-text ios

5
推荐指数
1
解决办法
258
查看次数

CTFontGetGlyphsForCharacters总是返回false

请帮我理解以下代码的问题:

NSString *fontName = @"ArialMT";
CGFloat fontSize = 20.0;
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)fontName, fontSize, NULL);
NSString *characters = @"ABC";
NSUInteger count = characters.length;
CGGlyph glyphs[count];
if (CTFontGetGlyphsForCharacters(fontRef, (const unichar*)[characters cStringUsingEncoding:NSUTF8StringEncoding], glyphs, count) == false)
    NSLog(@"*** CTFontGetGlyphsForCharacters failed.");
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

core-text ios

5
推荐指数
1
解决办法
1640
查看次数

检测点击的字符.UITextView characterRangeAtPoint始终返回nil

我正在试图确定,哪个特定字符UITextView已被挖掘.我试着用characterRangeAtPoint:方法.但它总是会返回零,无论UITextView我在哪里点击.我甚至编写并运行以下代码:

for (int x = 0; x<1000; x++) {
    pos.x = x;
    for (int y = 0; y<1000; y++) {
        pos.y = y;
        UITextRange * range = [textView characterRangeAtPoint:pos];
        if (range!=nil) {
            NSLog(@"x: %f, y: %f", pos.x, pos.y);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

好吧,它永远不会到达NSLog字符串.我究竟做错了什么?

iphone objective-c uikit core-text ios

5
推荐指数
2
解决办法
2032
查看次数

如何使用 CTFontCreateWithGraphicsFont 避免内存泄漏?

我已经将泄漏问题减少到这个易于编译的代码中,该代码在CTFontCreateWithGraphicsFont使用和发布后显示ct_fontcg_font将留下一个额外的引用。这是 Apple 内部引用计数问题还是我错过了一些东西,例如必须双重发布cg_font或更改发布顺序?谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <ApplicationServices/ApplicationServices.h>

int main(int argc, char **argv) {
    FILE *f = fopen("/Library/Fonts/Tahoma.ttf", "rb");
    fseek(f, 0, SEEK_END);
    long fsize = ftell(f);
    fseek(f, 0, SEEK_SET);
    
    char* font = (char*)malloc(fsize);
    fread(font, fsize, 1, f);
    fclose(f);
    
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, font, fsize, NULL);
    CGFontRef cg_font = CGFontCreateWithDataProvider(provider);
    CTFontRef ct_font = CTFontCreateWithGraphicsFont(cg_font, 36., NULL, NULL);
    CGDataProviderRelease(provider);

    //
    
    CFRelease(ct_font);
    CFRelease(cg_font);
    
    printf("ct_font: %d\ncg_font: %d\n", (int)CFGetRetainCount(ct_font), (int)CFGetRetainCount(cg_font));

    free(font);
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译运行后的结果:

ct_font: …

c macos refcounting core-text

5
推荐指数
1
解决办法
477
查看次数

获取 UILabel 中的每一行文本

我正在尝试将 aUILabel中的每一行添加到数组中,但我使用的代码似乎并不十分准确。

func getLinesArrayOfStringInLabel(label:UILabel) -> [String] {

    guard let text: NSString = label.text as? NSString else { return [] }
    let font:UIFont = label.font
    let rect:CGRect = label.frame

    let myFont: CTFont = CTFontCreateWithName(font.fontName as CFString, font.pointSize, nil)

    let attStr:NSMutableAttributedString = NSMutableAttributedString(string: text as String)
    attStr.addAttribute(NSAttributedStringKey.font, value:myFont, range: NSMakeRange(0, attStr.length))

    let frameSetter:CTFramesetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString)

    let path: CGMutablePath = CGMutablePath()
    path.addRect(CGRect(x: 0, y: 0, width: rect.size.width, height: 100000))

    let frame:CTFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil)
    let lines …
Run Code Online (Sandbox Code Playgroud)

core-text ios swift

5
推荐指数
1
解决办法
2491
查看次数

如何从表中获取字距对的值?

我得到了 CGGlyph

let a = CTFontGetGlyphWithName(font, "A" as CFString)
let v = CTFontGetGlyphWithName(font, "V" as CFString)
Run Code Online (Sandbox Code Playgroud)

我得到了一张桌子的链接

let kernData = CTFontCopyTable(font, CTFontTableTag(kCTFontTableKern), CTFontTableOptions(rawValue: 0))
Run Code Online (Sandbox Code Playgroud)

字距调整表

let kerningTable = CFDataGetBytePtr(kernData)
Run Code Online (Sandbox Code Playgroud)

macos core-text ios swift

5
推荐指数
0
解决办法
172
查看次数

为什么对NSMutableAttributedString addAttributes的调用会崩溃?

我在addAttributes无法重现的栏位(EXC_BREAKPOINT)上经常出车祸。在我传递的NSRange上添加了检查,令人惊讶的是它仍然崩溃...非常感谢收到任何想法!

let boldAttributes : [NSAttributedStringKey: Any] = [.font: cellStyle.boldFont()]
if (nsrange.location >= 0 && nsrange.length > 0 && nsrange.location + nsrange.length <= myAttributedString.length) {
    myAttributedString.addAttributes(boldAttributes, range: nsrange)
}
Run Code Online (Sandbox Code Playgroud)

通过请求添加的崩溃日志:

#0. Crashed: com.apple.main-thread
0  MyApp                   0x10054ae38 specialized UIAccessorizedTextField.accessoryAttributedString(IndexPath, cellStyle : UIAccessorizedTextFieldCellStyle) -> NSAttributedString (UIAccessorizedTextField.swift:322)
1  MyApp                   0x10054b104 specialized UIAccessorizedTextField.collectionView(UICollectionView, cellForItemAt : IndexPath) -> UICollectionViewCell (UIAccessorizedTextField.swift:345)
2  MyApp                   0x10054860c @objc UIAccessorizedTextField.collectionView(UICollectionView, cellForItemAt : IndexPath) -> UICollectionViewCell (UIAccessorizedTextField.swift)
3  UIKit                          0x18b229f3c -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 356
4  UIKit                          0x18bb90b68 -[UICollectionView _prefetchItemsForVelocity:maxItemsToPrefetch:invalidateCandidatesOnDirectionChanges:] + …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring core-text ios swift

5
推荐指数
1
解决办法
547
查看次数