到目前为止,我的所有研究似乎表明不可能准确地做到这一点.我在一开始就可以选择的唯一两个选项是:
a)使用CATextLayer的布局管理器 - 从4.0版开始在iOS上不可用
b)使用sizeWithFont:constrainedToSize:lineBreakMode:并根据此处返回的大小调整CATextLayer的帧.
选项(b)是最简单的方法,应该有效.毕竟,它与UILabels完美配合.但是当我将相同的帧计算应用于CATextLayer时,框架总是比预期或需要的要大一些.
事实证明,CATextLayers和UILabels中的行间距(对于相同的字体和大小)是不同的.因此,sizeWithFont(其行间距计算将与UILabels的行间距计算匹配)不会返回CATextLayers的预期大小.
通过使用UILabel打印相同的文本,与CATextLayer相比,并比较结果,进一步证明了这一点.第一行中的文本完全重叠(它是相同的字体),但CATextLayer中的行间距比UILabel略短.(对不起,我现在无法上传截图,因为我已经包含机密数据,我目前没有时间制作一个示例项目来获取干净的截图.我稍后会为后人上传它们,当时我有时间)
这是一个奇怪的区别,但我认为可以通过为我在那里使用的NSAttributedString指定适当的属性来调整CATextLayer中的间距,但似乎并非如此.查看CFStringAttributes.h我找不到可能与行间距相关的单个属性.
底线:
因此,在需要适合其文本的图层的情况下,似乎无法在iOS上使用CATextLayer.我是对的,还是我错过了什么?
PS:
我想使用CATextLayer和NSAttributedString的原因是因为要显示的字符串在不同点上的颜色不同.我想我必须一如既往地回去手工绘制字符串......当然总是可以选择从sizeWithFont中获取结果以获得正确的行高.
滥用"代码"标签稍微有点使帖子更具可读性.
我无法使用'CATextLayer'标记帖子 - 令人惊讶的是目前还没有这样的标签.如果有足够声誉的人碰到这篇文章,请相应标记.
编辑:最奇怪的事情:似乎从完整的应用程序运行此代码一切正常,但我总是从我的单元测试运行电影的创建,只有它没有工作.试图弄清楚为什么......
我正在尝试使用AVMutableComposition合并视频+音频+文本并将其导出到新视频.
我的代码基于WWDC '10的AVEditDemo
我在CATextLayer中添加了一个紫色背景,所以我可以知道它被导出到电影中,但没有显示文字......我尝试使用各种字体,位置,颜色定义,但没有任何帮助,所以我决定在这里发布代码,看看是否有人偶然发现了类似的东西,并告诉我我错过了什么.
这是代码(self.audio和self.video是AVURLAssets):
CMTime exportDuration = self.audio.duration;
AVMutableComposition *composition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *videoTrack = [[self.video tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
// add the video in loop until the audio ends
CMTime currStartTime = kCMTimeZero;
while (CMTimeCompare(currStartTime, exportDuration) < 0) {
CMTime timeRemaining = CMTimeSubtract(exportDuration, currStartTime);
CMTime currLoopDuration = self.video.duration;
if (CMTimeCompare(currLoopDuration, timeRemaining) > 0) {
currLoopDuration = timeRemaining;
}
CMTimeRange currLoopTimeRange = CMTimeRangeMake(kCMTimeZero, currLoopDuration);
[compositionVideoTrack insertTimeRange:currLoopTimeRange ofTrack:videoTrack
atTime:currStartTime error:nil];
currStartTime = CMTimeAdd(currStartTime, …Run Code Online (Sandbox Code Playgroud) 我使用以下代码创建UILabel并CATextLayer在我的应用程序中
- (void)viewDidLoad
{
[super viewDidLoad];
self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 90, 20, 20)];
self.textLabel.text = @"1";
self.textLabel.font = [UIFont systemFontOfSize:12];
self.textLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:self.textLabel];
self.textLayer = [CATextLayer layer];
[self.textLayer setString:@"1"];
[self.textLayer setFont:(__bridge CFTypeRef)([UIFont systemFontOfSize:12])];
self.textLayer.backgroundColor = [UIColor greenColor].CGColor;
self.textLayer.frame = CGRectMake(70, 90, 50, 20);
[self.view.layer addSublayer:self.textLayer];
}
Run Code Online (Sandbox Code Playgroud)
结果是

红色的是UILabel,绿色的是CALayer. I want to know how to vertical align the text inCALayer , as theUILabel`显示.
我想为我的图像创建一个文本叠加层.问题是,如果生病,尝试添加第二个文本,如字幕,它会忽略我的字体大小.
titleLayer.frame = CGRectMake(0, 80, imageView.bounds.width, 50)
subTitleLayer.frame = CGRectMake(0, 130, imageView.bounds.width, 40)
titleLayer.string = "Title"
subTitleLayer.string = "Subtitle"
let fontName: CFStringRef = "HelveticaNeue"
let fontSubName: CFStringRef = "HelveticaNeue-Thin"
titleLayer.font = CTFontCreateWithName(fontName, 16, nil)
subTitleLayer.font = CTFontCreateWithName(fontSubName, 10, nil) // Ignores the font-size
imageView.layer.addSublayer(titleLayer)
imageView.layer.addSublayer(subTitleLayer)
Run Code Online (Sandbox Code Playgroud)
新字体是正确的,但它总是与titleFont一样大小(16).如何更改字体大小?
我正在使用CATextLayer,为了在iOS中使用自定义字体,我知道有简单的方法来使用自定义字体,Fonts provided by application但这是不同的字体.我想知道有没有办法改变每个角色之间的间距?我没有找到任何财产这样做!
编辑:
- (void)viewWillAppear:(BOOL)animated {
CTFontRef font = [self newCustomFontWithName:@"yeki"
ofType:@"ttf"
attributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:16.f]
forKey:(NSString *)kCTFontSizeAttribute]];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
normalTextLayer_ = [[CATextLayer alloc] init];
normalTextLayer_.font = font;
normalTextLayer_.string = str;
normalTextLayer_.wrapped = YES;
normalTextLayer_.foregroundColor = [[UIColor purpleColor] CGColor];
normalTextLayer_.fontSize = 50.f;
normalTextLayer_.alignmentMode = kCAAlignmentRight;
normalTextLayer_.frame = CGRectMake(0.f,100.f, screenBounds.size.width, screenBounds.size.height /1.f);
[self.view.layer addSublayer:normalTextLayer_];
CFRelease(font);
}
Run Code Online (Sandbox Code Playgroud) 如果我设置textLayer.wrapped = YES,如何调整大小textLayer以包含包装的文本?即,我如何获得新的高度textLayer?
基本上,我想要类似的东西-[UILabel sizeToFit].
我有CATextLayer清洁问题.和iOS一起解决方案是:
textLayer.contentsScale = UIScreen.mainScreen.scale
Run Code Online (Sandbox Code Playgroud)
但我无法在可可NSScreen上找到"规模"属性
注意:使用Object-C或Swift的答案都可以.
我有一个带有多行文字的CATextLayer.包装选项设置为YES.但它没有将文本包装在最后一行.如果我用\n和空格添加另一行,则会发生换行(但这会破坏布局).
这只发生在iOS> = 8.3中
有什么想法会导致这种情况吗?
我对CATextlayer有问题。我为CATextlayer设置了包装== YES,并为此自动设置了多行。但是行距很小,看起来很糟糕。有没有办法设置CATextlayer的行距?谢谢。
我有一个UIView已创建的自定义,它具有多个自定义UIControl实例,这些实例是非矩形按钮(而不是使用UIButton实例)。之所以这样做,是因为我需要能够绘制非矩形按钮,并使它们仅在非矩形区域对水龙头做出反应。
因为UIControl不附带UILabel,所以我使用CATextLayer来添加此功能。我希望我正在绘制的文本(通过将设置为layer.string等于NSAttributedString)应用阴影,但是它似乎CATextLayer忽略了任何NSShadow属性。
我找不到任何文档可以告诉我任何一种方法,但是实际上,以下操作不起作用:
NSMutableParagraphStyle *s = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
s.lineSpacing = 0.9f;
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor orangeColor];
shadow.shadowOffset = CGSizeMake(10,10);
NSDictionary *textAttrs = @{NSFontAttributeName : [UIFont someFont],
NSParagraphStyleAttributeName : [s copy],
NSShadowAttributeName : shadow};
Run Code Online (Sandbox Code Playgroud)
也就是说CATextLayer,除了,显示的内容与您预期的一样NSShadow。因此,以上代码与该代码完全相同(据我所知):
NSMutableParagraphStyle *s = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
s.lineSpacing = 0.9f;
NSDictionary *textAttrs = @{NSFontAttributeName : [UIFont someFont], …Run Code Online (Sandbox Code Playgroud) catextlayer ×10
ios ×6
iphone ×2
swift ×2
uilabel ×2
avfoundation ×1
calayer ×1
cocoa ×1
core-text ×1
ios4 ×1
macos ×1
nsshadow ×1
objective-c ×1
sizetofit ×1
sizewithfont ×1
uiimageview ×1
word-wrap ×1
xcode ×1