I am trying to set the height of the multiline label in OSX but when i use sizetofit functionality it cuts the line width and doesn't set the height.What i want is to be able to grow multi line label as its size increases automatically setting the height and the width.This is the code i am working on right now.
self.solutionTextView.stringValue = String(format: "asjdasjdhsjf dasfjashd fjadhsf sfsdjfijijij dsfsd jsj aidjfajs jfsajf isdj sjdaofjaisdjf i dsfisdfjsdjfi sj jasof sjdf ijsdj fisjdsj fjas jsid isdf jsdi fsiajf isjfi jdsjf saof jisadfj isjdfisdj foajdfjsdawei difj jhjsahhjashashhjsahsahjhsahjsahjshajhsahsahjhjsahjsahjhjsahsahjsajhjhashjshjhjsahsjahjsahjsahsahjsahjshahsjahjsahjsahjshhsjahjsahjsahjsahjhjsahjsahsahhsahjsahsahhsajhjsahjsahjhsjahjsahjsahjhsjahjahjsahjshjahjsahjhjahjsahjhsjahjshjshajhjsahjsahjshjahjsahjsahjshjahjsahjshjahjsahjsahsahhjsahjsahjsahjsahjhjsahjahjsahhsahjjasjhhjsahjsahhjsahjhjsahjsajhhjsahjhjsahjsahjhjsahjshjahjsahjsahjjhashjshajhahjahjhsahhajshjashahjshhjahj%#", self.recommendation.valueForKey("solution") as String)
self.solutionTextView.sizeToFit()
self.linksTextView.stringValue = String(format: "First Link %#", self.recommendation.valueForKey("tip1") as String)
// self.linksTextView.sizeToFit()
self.proTipTextView.stringValue = String(format: "Pro Tip: Not available")
I am developing on iOS and although your variable is named 'self.solutionTextView' I assume we are talking about the equivalent of a UILabel. In this case you could measure the text and use this as an frame:
let frame = self.solutionTextView.frame
frame.size.height = NSString(string:self.solutionTextView.stringValue).sizeWithAttributes([NSFontAttributeName : self.solutionTextView.font]).height
self.solutionTextView.frame = frame
I also assume you have a set width and are more interested in scaling the label vertically in height. If not, the method above returns a size which you can use to get the width.
Related
El Capitan introduced San Francisco system font, which has proportional digits by default.
This makes numbers in table columns look jagged and hard to compare:
I'd like to enable fixed-width numbers option for the font, but keep using the default system font and keep backwards compatibility with earlier versions of OS X.
In Interface Builder selecting font > Font Panel > Typography > Monospaced Numbers does not affect the font (XIB file remains unchanged).
What's the right way to set monospaced numbers in OS X table view columns? (I suspect IB is unusable for this, so a programmatic solution is OK too).
Just use +[NSFont monospacedDigitSystemFontOfSize:weight:] when it's available. It's new in 10.11, but still not in the NSFont docs. It's in the headers and was discussed in the WWDC 2015 videos. So, something like:
if ([NSFont respondsToSelector:#selector(monospacedDigitSystemFontOfSize:weight:)])
textField.font = [NSFont monospacedDigitSystemFontOfSize:textField.font.pointSize weight:NSFontWeightRegular];
Here's a Swift extension that gives you a monospaced digits font with high legibility.
extension NSFont {
var legibleNumbersVariant: NSFont {
let features = [
[NSFontFeatureTypeIdentifierKey: kNumberSpacingType,
NSFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector],
[NSFontFeatureTypeIdentifierKey: kStylisticAlternativesType,
NSFontFeatureSelectorIdentifierKey: kStylisticAltSixOnSelector]
]
let descriptor = fontDescriptor.addingAttributes([NSFontFeatureSettingsAttribute: features])
return NSFont(descriptor: descriptor, size: pointSize) ?? self
}
}
Treat the following as pseudo-code, quickly done, not throughly tested, etc.
Given an NSFont which represents a font which has monospaced numbers as a feature the following method will produce another NSFont with that feature selected:
- (NSFont *) newMonospaceNumbersFont:(NSFont *)font
{
CTFontDescriptorRef origDesc = CTFontCopyFontDescriptor((__bridge CTFontRef)font);
CTFontDescriptorRef monoDesc = CTFontDescriptorCreateCopyWithFeature(origDesc, (__bridge CFNumberRef)#(kNumberSpacingType), (__bridge CFNumberRef)#(kMonospacedNumbersSelector));
CFRelease(origDesc);
CTFontRef monoFont = CTFontCreateWithFontDescriptor(monoDesc, font.pointSize, NULL);
CFRelease(monoDesc);
return (__bridge_transfer NSFont *)monoFont;
}
You can use this, say, to take the current font of a UI element and convert it to one with monospace numbers.
HTH
Variant for Swift
Assuming res is the NSTextField with the number to display:
let origDesc = CTFontCopyFontDescriptor(res.font!)
let monoDesc = CTFontDescriptorCreateCopyWithFeature(origDesc, kNumberSpacingType, kMonospacedNumbersSelector)
let monoFont = CTFontCreateWithFontDescriptor(monoDesc, res.font!.pointSize, nil)
res.font = monoFont
In my experience, the "font panel" functionality isn't well defined and I usually just ignore it whenever I'm messing with a XIB or Storyboard.
What should work is to go back to that "Font" attribute in the Text Field Cell attributes inspector and then select "User Fixed Pitch" from the Font drop down menu (the choice should automatically default to size 11).
If you bump the font size up a point, it'll magically switch to Monaco (the default fixed width font).
I'm trying to specify the number of lines for NSTextView. My designer is requesting 2 lines of text max. I've tried NSMutableParagraph style to add the ellipses truncation that I want, but with NSMutableParagraph I can only get NSTextView with 1 line and without NSMutableParagraph, I get a scrolling text with as many lines as needed to complete text.
var attributedString = NSMutableAttributedString(string: "This is my text, I can keep going for many characters")
var para = NSMutableParagraphStyle()
para.lineBreakMode = NSLineBreakMode.ByTruncatingTail
let globalAttributes = [
NSParagraphStyleAttributeName: para
]
let range = NSRange(location:0, length: attributedString.length)
attributedString.addAttributes(globalAttributes, range: range)
cellView.myTextView!.textStorage?.setAttributedString(attributedString)
I've tried height constraint on NSTextView. I've tried:
cellView.myTextView!.textContainer?.containerSize = NSMakeSize(300, 32)
I've tried creating IBOutlet for NSScrollView that NSTextView in within and adjusting its height. No luck with getting both 2 lines and truncation. Any help is greatly appreciated. I feel like I'm just missing a method or setup. Thanks!
From 10.11 you can use this
yourTextViewObj.textContainer.maximumNumberOfLines = 2;
You can use an NSTextField configured as a multi-line label. That means setting its cell's wraps property to true and, if desired, its truncatesLastVisibleLine to true.
For NSTextField (aka label) You can just do self.textField.maximumNumberOfLines = 2;
That's it.
Max number of lines is now a property of NSTextField
label.maximumNumberOfLines = 1;
How can I use different system fonts for the line //pointsLabel.font = FontHUD, no custom font but different fonts available in the system
//"points" label
var pointsLabel = UILabel(frame: CGRectMake(ScreenWidth-340, 30, 140, 70))
pointsLabel.backgroundColor = UIColor.clearColor()
//pointsLabel.font = FontHUD
pointsLabel.text = " Points:"
self.addSubview(pointsLabel)
You could use UIFont.preferredFontForTextStyle(style: String) and pass any of the following as the style:
UIFontTextStyleHeadline
UIFontTextStyleSubheadline
UIFontTextStyleBody
UIFontTextStyleFootnote
UIFontTextStyleCaption1
UIFontTextStyleCaption2
On a bit of a side note - an advantage of using preferredFontForTextStyle is that you can use it to make your app support Dynamic Type because the size of the font returned varies depending on the user's preferred text size (set in the Settings app under Display & Brightness -> Text Size). To fully support dynamic type you should listen for changes in the preferred font size using NSNotifcationCenter and observing UIContentSizeCategoryDidChangeNotification, then updating your labels/textviews appropriately.
I'm bit surprised, because I'd like to change a label and a textfield in dynamic during my App execution. I mean based on an image size, I need to change label and textfield position. But, it doesn't work.
I did all the creation in the main window file (ex xib).
Then when I'm clicking on my segment selector I'm doing this (and my field and label are nt moving !?):
CGRect frame = [LHeight frame];
frame.origin.x += 100; // change the location
frame.size.width += 100; // change the size
[LHeight setFrame:frame];
Even this method does't work :
LHeight.frame = CGRectMake(LHeight.frame.origin.x + 100, LHeight.frame.origin.y, LHeight.frame.size.width + 100, LHeight.frame.size.height);
is any parameters to change in the XIB file ?
thanks in advance for your support.
Regards
LHeight.frame = CGRectMake(LHeight.frame.origin.x + 100, LHeight.frame.origin.y, LHeight.frame.size.width + 100, LHeight.frame.size.height);
I am trying to create an animation to make it look like a button turns over and the back shows. So what I was trying to do is:
1- Show a button with BackgroundColor x. (The button now has a Width of null, the property ActualWidth does have a value.)
2- Create a double animation that changes the width of the button to zero.
DoubleAnimation widthAnimation = new DoubleAnimation();
widthAnimation.From = this.ActualWidth;
widthAnimation.To = 0;
widthAnimation.SpeedRatio = 3;
widthAnimation.Duration = TimeSpan.FromMilliseconds(800);
3- Change the BackgroundColor of the button.
ColorAnimation colorAnimation = new ColorAnimation();
colorAnimation.From = State ? _xColor : _yColor;
colorAnimation.To = State ? _yColor : _xColor;
colorAnimation.BeginTime = TimeSpan.FromMilliseconds(400);
colorAnimation.Duration = TimeSpan.Zero;
4- Change the width back to it's original value.
widthAnimation.AutoReverse = true;
The problem is when the animation runs twice the animation reads this.ActualWidth while animating, which causes it to fail to the original width. How can I solve this? I would like to set the Width back to null again, but it seems impossible to me.
You'd better use xaml style and template to "declare" what you want and let WPF/Silverlight take care of all.
If you try to do the same thing by code you can do it but you need to know what the framework does behind the scenes.
Basically you can set
- Style to define the values of some properties of the control
- DataTemplate to define the visual representation of the control's content
- ControlTemplate to define the appearance of the control
Each of those can have Triggers
- Property Triggers
to set properties or starts actions, such as an animation
when a property value changes or when an event is raised;
EventTriggers and Storyboards
to start a set of actions based on the occurrence of an event
If you like to learn about XAML Style and Template,
take a look at http://msdn.microsoft.com/en-us/library/ms745683.aspx
Spend a day to learn and save many hours (or days) of try and error and frustration!
To go right to the point, in your case I think you should use a Storyboard.
See http://msdn.microsoft.com/en-us/library/ms742868.aspx
where you can find also the code equivalent of XAML examples
I came to the idea to targetting the MaxWidth instead of the actual Width. I now use a KeyFrameCollection which sets the MaxWidth to int.MaxValue at the start (so also at the end when using autoreverse).
It will work fine untill there will be phones with a resolution bigger than the max int value.
The code:
DoubleAnimationUsingKeyFrames widthAnimation = new DoubleAnimationUsingKeyFrames();
widthAnimation.KeyFrames.Add(new DiscreteDoubleKeyFrame()
{
KeyTime = TimeSpan.Zero,
Value = int.MaxValue,
});
widthAnimation.KeyFrames.Add(new LinearDoubleKeyFrame()
{
KeyTime = TimeSpan.FromMilliseconds(1),
Value = ActualWidth,
});
widthAnimation.KeyFrames.Add(new LinearDoubleKeyFrame()
{
KeyTime = TimeSpan.FromMilliseconds(400),
Value = 0,
});
widthAnimation.Duration = TimeSpan.FromMilliseconds(400);
widthAnimation.AutoReverse = true;