小编Ste*_*ghe的帖子

行间距如何在Core Text中工作?(为什么它与NSLayoutManager不同?)

我正在尝试使用Core Text函数绘制文本,其行间距尽可能接近使用NSTextView时的行间距.

以此字体为例:

NSFont *font = [NSFont fontWithName:@"Times New Roman" size:96.0];
Run Code Online (Sandbox Code Playgroud)

如果我在NSTextView中使用它,这个字体的行高是111.0.

NSLayoutManager *lm = [[NSLayoutManager alloc] init];
NSLog(@"%f", [lm defaultLineHeightForFont:font]); // this is 111.0
Run Code Online (Sandbox Code Playgroud)

现在,如果我使用Core Text做同样的事情,结果是110.4(假设您可以通过添加上升,下降和前导来计算线高).

CTFontRef cFont = CTFontCreateWithName(CFSTR("Times New Roman"), 96.0, NULL);
NSLog(@"%f", CTFontGetDescent(cFont) + CTFontGetAscent(cFont) + 
             CTFontGetLeading(cFont)); // this is 110.390625
Run Code Online (Sandbox Code Playgroud)

这非常接近111.0,但对于某些字体,差异要大得多.例如,对于Helvetica,NSLayoutManager给出115.0而CTFont上升+下降+领先= 96.0.很明显,对于Helvetica,我无法使用上升+下降+导致计算线之间的间距.

所以我想我会使用CTFrame和CTFramesetter来布局几行并从中获取行间距.但这也给出了不同的价值观.

CTFontRef cFont = CTFontCreateWithName(CFSTR("Times New Roman"), 96.0, NULL);
NSDictionary *attrs = [NSDictionary dictionaryWithObject:(id)cFont forKey:(id)kCTFontAttributeName];
NSAttributedString *threeLines = [[NSAttributedString alloc] initWithString:@"abcdefg\nabcdefg\nabcdefg" attributes:attrs];

CTFramesetterRef threeLineFramesetter =  CTFramesetterCreateWithAttributedString((CFAttributedStringRef)threeLines);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0.0, …
Run Code Online (Sandbox Code Playgroud)

macos objective-c nslayoutmanager nstextview core-text

26
推荐指数
1
解决办法
1万
查看次数

UITabel在具有自动布局的UITableViewCell中具有错误的高度

我有一个UITableView固定高度为100点的细胞.单元格在xib文件中创建,该文件使用3个约束将a固定UILabel到单元格的左,右和上边缘contentView.标签的垂直拥抱优先级设置为1000,因为我希望单元格的高度尽可能小.

当xib文件中单元格的宽度设置为320磅,与iPhone上的tableView宽度相同时,autolayout按预期工作.但是,当我将单元格的宽度设置为小于320点时,我得到了意想不到的结果.(我想在tableViews中使用具有不同宽度的相同单元格,例如在通用应用程序中)

例如:当我将宽度设置为224点并为标签提供一个占据该宽度的2行的文本时,标签的高度将增加以适应2行,但是当单元格调整为320点以适应一个tableView的宽度,文本只占1行,但标签的高度保持2行.

我在GitHub上放了一个示例项目来演示这个问题:https://github.com/bluecrowbar/CellLayout

有没有办法让UILabel总是调整大小以拥抱其文本内容?

uitableview ios autolayout

22
推荐指数
2
解决办法
2万
查看次数

UIBarButtonItem:如何更改文本阴影偏移量?

我正试图改变文本背后阴影的偏移量UIBarButtonItem.

这是我的代码:

NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[UIColor colorWithWhite:0.30 alpha:1.0] forKey:UITextAttributeTextColor];
[attributes setValue:[UIColor whiteColor] forKey:UITextAttributeTextShadowColor];
[attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

更改文本颜色有效.更改阴影颜色有效.更改阴影偏移似乎没有任何作用.

我这样做的方式有问题吗?我也试过直接设置它,没有外观代理,但这也不起作用.

cocoa-touch uibarbuttonitem ios

8
推荐指数
1
解决办法
3652
查看次数

Immutable.js记录(默认)唯一ID

我想创建具有(或多或少)唯一键的Immutable.js记录.像这样的东西:

import { Record } from 'immutable'

var MyRecord = Record({
    key: Math.random().toString(),
    name: ""
})
Run Code Online (Sandbox Code Playgroud)

这可能吗?我试过了,所有记录都有相同的密钥.我是这样导入的MyRecord:

import { MyRecord } from '../model/MyRecord'
Run Code Online (Sandbox Code Playgroud)

并创建这样的新记录

var r = new MyRecord(data)
Run Code Online (Sandbox Code Playgroud)

哪里data是json对象.

我当然可以在创建新记录后手动添加密钥,但我更愿意找到一种自动化方法.

javascript immutable.js

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

Xcode 6:具有透明度的pdf矢量图像

Xcode 6现在支持资产目录中基于pdf矢量的图像.是否可以创建具有透明度的基于pdf矢量的图像?

pdf ios xcode6

4
推荐指数
1
解决办法
1635
查看次数