cocoa – NSButton setAlignment不起作用

cocoa – NSButton setAlignment不起作用,第1张

概述我用了 [button setAlignment:NSCenterTextAlignment]; 使文本显示在按钮的中心. 有效. 但是如果我在代码之前设置按钮标题属性,按钮’setAlignment’将不起作用 - (void)setButtonTitle:(NSButton*)button fontName:(NSString*)fontName fontSize:(CGFloat)fontS 我用了

[button setAlignment:NSCenterTextAlignment];

使文本显示在按钮的中心.

有效.

但是如果我在代码之前设置按钮标题属性,按钮’setAlignment’将不起作用

- (voID)setbuttonTitle:(NSbutton*)button Fontname:(Nsstring*)Fontname FontSize:(CGfloat)FontSize Fontcolor:(NScolor*)Fontcolor;{    NSMutableAttributedString *attributedString =    [[NSMutableAttributedString alloc] initWithString:[button Title]                                     attributes:[NSDictionary dictionaryWithObject:[NSFont FontWithname:Fontname size:FontSize]                                                                            forKey:NSFontAttributename]];    [attributedString addAttribute:NSForegroundcolorAttributename                               value:Fontcolor                               range:NSMakeRange(0,[[button Title] length] )];    [button setAttributedTitle: attributedString];    [button setAlignment:NSCenterTextAlignment];//button Title alignment always displayed as 'NSleftTextAlignment' rather than 'NSCenterTextAlignment'.}

标题对齐始终显示为“NSleftTextAlignment”而不是“NSCenterTextAlignment”.

欢迎任何评论

解决方法 由于您使用按钮标题的属性字符串,因此该字符串中的属性负责设置对齐.

要使该属性字符串居中,请添加具有居中对齐值的NSParagraphStyleAttributename属性:

NSMutableParagraphStyle *centredStyle = [[[NSParagraphStyle defaultParagraphStyle] mutablecopy] autorelease];[centredStyle setAlignment:NSCenterTextAlignment];NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,NSParagraphStyleAttributename,[NSFont FontWithname:Fontname size:FontSize],NSFontAttributename,Fontcolor,NSForegroundcolorAttributename,nil];NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:[button Title]                                 attributes:attrs];[button setAttributedTitle: attributedString];

在上面的代码中,我创建了一个包含属性字符串的所有属性的单个attrs字典.从您的代码看,无论如何,字体颜色应该应用于整个字符串.

总结

以上是内存溢出为你收集整理的cocoa – NSButton setAlignment不起作用全部内容,希望文章能够帮你解决cocoa – NSButton setAlignment不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1034229.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存