[英]Font change on selecting NSTextField
I am wanting to create a simple label with an attributed string. 我想创建一个带有属性字符串的简单标签。 I do this like this:
我是这样做的:
NSDictionary *noNotificationsAttrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
NSParagraphStyleAttributeName,
[NSFont fontWithName:@"Raleway-Bold" size:30],
NSFontAttributeName,
_grey,
NSForegroundColorAttributeName,
nil];
NSMutableAttributedString *noNotificationsString =
[[NSMutableAttributedString alloc] initWithString:@"No Notifications"
attributes:noNotificationsAttrs];
NSTextField* title_field = [[NSTextField alloc] initWithFrame:
CGRectMake(
0,
0,
200,
200
)
];
[title_field setWantsLayer:true];
[title_field setSelectable:YES];
[title_field setAllowsEditingTextAttributes:true];
[title_field setAttributedStringValue:noNotificationsString];
[title_field setEditable:false];
[title_field setBordered:false];
title_field.tag = 1;
Which turns out like this: 结果是这样的:
and 和
Unfortunately when clicking (selecting) this label it appears like this: 不幸的是,当点击(选择)此标签时,它显示如下:
and 和
Which is a bit bolder and pixelated around the corners. 在角落周围有点大胆和像素化。 This is happening with lots of other labels with different strings, sizes and colours.
这种情况正在发生,许多其他标签具有不同的字符串,大小和颜色。 How can I fix it?!
我该怎么办呢?!
Please note these labels are nested inside nsscrollview
-> nstableview
-> viewForTableColumn
请注意,这些标签嵌套在
nsscrollview
- > nstableview
- > viewForTableColumn
I believe the problem is that NSCell calls an edit function on mousedown.
我相信问题是NSCell在mousedown上调用了一个编辑功能。
The font is also different on selection!! 选择的字体也不同!!
Interestingly if I remove wantslayer:YES
from the (2) parent view it does not do this. 有趣的是,如果我从(2)父视图中删除
wantslayer:YES
,它就不会这样做。 But they both need wantslayer or else I can't have curved corners etc... 但他们都需要想要层,否则我就不能有弯角......
Add this line to your code. 将此行添加到您的代码中。
NSTextView *textEditor = (NSTextView *)[[[NSApplication sharedApplication] keyWindow] fieldEditor:YES forObject:title_field];
[textEditor setSelectedTextAttributes:noNotificationsAttrs];
Thus adding the attributes to NSTextView associated with window while selection. 因此,在选择时将属性添加到与窗口关联的NSTextView。
What you're getting now like bold and font changes after selection will get overrided. 你现在得到的是粗体和选择后的字体更改将被覆盖。
Edit: 编辑:
Have changed NSForegroundColorAttributeName
to NSBackgroundColorAttributeName
and _grey
to [NSColor clearColor]
NSForegroundColorAttributeName
NSBackgroundColorAttributeName
更改为NSBackgroundColorAttributeName
并将_grey
为[NSColor clearColor]
NSDictionary *noNotificationsAttrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
NSParagraphStyleAttributeName,
[NSFont fontWithName:@"Raleway-Bold" size:30],
NSFontAttributeName,
[NSColor clearColor],
NSBackgroundColorAttributeName,
nil];
To fix this I made a custom NSTextField where on select it updates like this: 为了解决这个问题,我做了一个自定义的NSTextField,在这里选择它更新如下:
- (void)textViewDidChangeSelection:(NSNotification *)a{
[self setNeedsDisplay:YES];
[self setNeedsLayout:YES];
[self.superview setNeedsLayout:YES];
[self.superview setNeedsDisplay:YES];
}
but still acts funny some times 但有时候仍然很有趣
I had fixed it perfect! 我把它修好了! hope to help everyone.
希望能帮助大家。
1.lazy property
- (NSTextField *)textFieldOne{
if (!_textFieldOne) {
_textFieldOne = ({
NSTextField *view = [[NSTextField alloc]init];
view.cell.scrollable = true;
view.font = [NSFont fontWithName:@"PingFangSC-Light" size:14];
view.cell.wraps = true;
view.editable = false;
view.selectable = true;
view.allowsEditingTextAttributes = true;
if (@available(macOS 10.12.2, *)) {
view.automaticTextCompletionEnabled = true;
} else {
// Fallback on earlier versions
}
view;
});
}
return _textFieldOne;
}
2.
// @{string: url}
NSDictionary *dic = @{
@"github/shang1219178163": @"https://github.com/shang1219178163",
};
self.textFieldOne.stringValue = [NSString stringWithFormat:@"%@\n%@\n%@", NSApplication.appName, NSApplication.appCopyright, @"github/shang1219178163"];
[self.textFieldOne setHyperlinkDic:dic];
3.
#import "NSTextField+Helper.h"
-(void)setHyperlinkDic:(NSDictionary *)dic{
// both are needed, otherwise hyperlink won't accept mousedown
NSTextField *textField = self;
NSDictionary * attributes = @{
NSFontAttributeName: textField.font,
};
__block NSMutableAttributedString *mattStr = [[NSMutableAttributedString alloc]initWithString:textField.stringValue attributes:attributes];
[dic enumerateKeysAndObjectsUsingBlock:^(NSString * key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSURL *url = [NSURL URLWithString:obj];
NSAttributedString * attStr = [NSAttributedString hyperlinkFromString:key withURL:url font:textField.font];
NSRange range = [mattStr.string rangeOfString:key];
[mattStr replaceCharactersInRange:range withAttributedString:attStr];
}];
textField.attributedStringValue = mattStr;
textField.cell.wraps = true;
textField.cell.scrollable = true;
textField.editable = false;
textField.selectable = true;
textField.allowsEditingTextAttributes = true;
}
4.
#import "NSAttributedString+Helper.h"
+(id)hyperlinkFromString:(NSString *)string withURL:(NSURL *)aURL font:(NSFont *)font{
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString: string];
NSRange range = NSMakeRange(0, attrString.length);
// NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
NSDictionary * dic = @{
NSFontAttributeName: font,
NSForegroundColorAttributeName: NSColor.blueColor,
NSLinkAttributeName: aURL.absoluteString,
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
// NSParagraphStyleAttributeName: paraStyle,
// NSBaselineOffsetAttributeName: @15,
};
[attrString beginEditing];
[attrString addAttributes:dic range:range];
[attrString endEditing];
return attrString;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.