Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This change implement UILabel minimumScale #1918

Merged
merged 4 commits into from
Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 62 additions & 21 deletions Frameworks/UIKit/UILabel.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#import "XamlControls.h"
#import "XamlUtilities.h"

static const wchar_t* TAG = L"UILabel";

@implementation UILabel {
idretaintype(NSString) _text;
idretaintype(UIFont) _font;
Expand All @@ -45,6 +47,9 @@ @implementation UILabel {
UILineBreakMode _lineBreakMode;
BOOL _adjustFontSize;
float _minimumFontSize;
float _originalFontSize;
BOOL _useMinimumScaleFactor;
float _minimumScaleFactor;
int _numberOfLines;
BOOL _isDisabled;
BOOL _isHighlighted;
Expand Down Expand Up @@ -129,11 +134,24 @@ - (void)_adjustFontSizeToFit {

// Special:on reference platform, adjustFontSizeToFit is no-op when lineBreakMode is Wrapping
if (_lineBreakMode != UILineBreakModeWordWrap && _lineBreakMode != UILineBreakModeCharacterWrap) {
float minimumFontSize = _minimumFontSize;

// if minimumScaleFactor is used, it should override _minimumFontSize which is deprecated
Copy link
Contributor

@jaredhms jaredhms Feb 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// if minimumScaleFactor is used, it should override _minimumFontSize which is deprecated [](start = 7, length = 90)

does the minimumFontSize property return a different value in this case? #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. they are different and separate and independent of each other.


In reply to: 99672980 [](ancestors = 99672980)

if (_useMinimumScaleFactor) {
if (_minimumScaleFactor > 0.0) {
minimumFontSize = _minimumScaleFactor * _originalFontSize;
} else {
// per reference platform, if minimumScaleFactor is set and equal to 0.0
// use current font size is used as smallest font size
minimumFontSize = [_font pointSize];
}
}

StrongId<UIFont> _targetFont = [self _findMaxFontSizeToFit:rect
Text:_text
Font:_font
NumberOfLines:_numberOfLines
MinimumFontSize:_minimumFontSize
MinimumFontSize:minimumFontSize
StartingFontSize:[_font pointSize]];
if (_targetFont != nil) {
// found a font that can be adjusted to fit, otherwise, do nothing
Expand All @@ -148,7 +166,7 @@ - (void)_updateXamlElement {
[_textBlock setText:_text];
[_textBlock setFontSize:[_font pointSize]];

WUTFontWeight* fontWeight =[WUTFontWeight new];
WUTFontWeight* fontWeight = [WUTFontWeight new];
fontWeight.weight = static_cast<unsigned short>([_font _fontWeight]);
[_textBlock setFontWeight:fontWeight];
[_textBlock setFontStyle:static_cast<WUTFontStyle>([_font _fontStyle])];
Expand Down Expand Up @@ -193,15 +211,33 @@ - (instancetype)initWithCoder:(NSCoder*)coder {
_font = font;

_alignment = (UITextAlignment)[coder decodeInt32ForKey:@"UITextAlignment"];
_adjustFontSize = [coder decodeInt32ForKey:@"UIAdjustsFontSizeToFit"];

if ([coder containsValueForKey:@"UINumberOfLines"]) {
_numberOfLines = [coder decodeInt32ForKey:@"UINumberOfLines"];
} else {
_numberOfLines = 1;
}

_minimumFontSize = [coder decodeFloatForKey:@"UIMinimumFontSize"];
// one of UIMinimumScaleFactor or UIMinimumFontSize or UIAdjustsFontSizeToFit has to be set
// if UIAdjustsFontSizeToFit is set, it must be NO.
// if UIMinimumScaleFactor or UIMinimumFontSize is set, UIAdjustsFontSizeToFit is dereived to be YES
if ([coder containsValueForKey:@"UIMinimumScaleFactor"]) {
_minimumScaleFactor = [coder decodeFloatForKey:@"UIMinimumScaleFactor"];
_useMinimumScaleFactor = YES;
_adjustFontSize = YES;
} else if ([coder containsValueForKey:@"UIMinimumFontSize"]) {
_minimumFontSize = [coder decodeFloatForKey:@"UIMinimumFontSize"];
_adjustFontSize = YES;
} else if ([coder containsValueForKey:@"UIAdjustsFontSizeToFit"]) {
_adjustFontSize = [coder decodeInt32ForKey:@"UIAdjustsFontSizeToFit"];
if(_adjustFontSize) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

( [](start = 14, length = 1)

nit: missing space

TraceWarning(TAG, L"Invalid nib format, UIAdjustsFontSizeToFit should be set to FALSE if it is set, currently set as TRUE, overwritting with FALSE");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overwritting [](start = 139, length = 12)

nit: typo

_adjustFontSize = NO;
}
} else {
TraceWarning(TAG, L"Invalid nib format, None of UIMinimumScaleFactor/UIMinimumFontSize/UIAdjustsFontSizeToFit is set. default UIAdjustsFontSizeToFit to FALSE");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default [](start = 130, length = 7)

nit defaulting

_adjustFontSize = NO;
}

if ([coder containsValueForKey:@"UILineBreakMode"]) {
_lineBreakMode = (UILineBreakMode)[coder decodeInt32ForKey:@"UILineBreakMode"];
Expand Down Expand Up @@ -262,8 +298,18 @@ - (void)_initUILabel {
// on reference platform, default minimum font size is zero but always slightly bigger than zero in reality acccording to the
// documentation
_minimumFontSize = 0.0001f;
_minimumScaleFactor = 0.0;
_useMinimumScaleFactor = NO;
_numberOfLines = 1;
[self setOpaque:FALSE];

_originalFontSize = [UIFont labelFontSize];
_font = [UIFont fontWithName:@"Segoe UI" size:_originalFontSize];

// TODO: Reevaluate whether or not this is the correct default mode for UILabels that are initialized via initWithFrame.
// Some of our test apps expect the initWithCoder path to default to UIViewContentModeScaleToFill (aka kCAGravityResize).
[self setContentMode:UIViewContentModeRedraw];
[self _updateXamlElement];
}

/**
Expand All @@ -272,14 +318,6 @@ - (void)_initUILabel {
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self _initUILabel];

// TODO: Reevaluate whether or not this is the correct default mode for UILabels that are initialized via initWithFrame.
// Some of our test apps expect the initWithCoder path to default to UIViewContentModeScaleToFill (aka kCAGravityResize).
[self setContentMode:UIViewContentModeRedraw];

_font = [UIFont fontWithName:@"Segoe UI" size:[UIFont labelFontSize]];

[self _updateXamlElement];
}

return self;
Expand All @@ -291,14 +329,6 @@ - (instancetype)initWithFrame:(CGRect)frame {
- (instancetype)initWithFrame:(CGRect)frame xamlElement:(WXFrameworkElement*)xamlElement {
if (self = [super initWithFrame:frame xamlElement:xamlElement]) {
[self _initUILabel];

// TODO: Reevaluate whether or not this is the correct default mode for UILabels that are initialized via initWithFrame.
// Some of our test apps expect the initWithCoder path to default to UIViewContentModeScaleToFill (aka kCAGravityResize).
[self setContentMode:UIViewContentModeRedraw];

_font = [UIFont fontWithName:@"Segoe UI" size:[UIFont labelFontSize]];

[self _updateXamlElement];
}

return self;
Expand Down Expand Up @@ -326,6 +356,7 @@ - (void)initAccessibility {
- (void)setFont:(UIFont*)font {
if (![_font isEqual:font]) {
_font = font;
_originalFontSize = [_font pointSize];
if (_adjustFontSize) {
[self _adjustFontSizeToFit];
} else {
Expand Down Expand Up @@ -496,6 +527,7 @@ - (NSInteger)numberOfLines {
*/
- (void)setMinimumFontSize:(float)size {
_minimumFontSize = size;
_useMinimumScaleFactor = NO;
}

/**
Expand Down Expand Up @@ -589,9 +621,18 @@ - (void)setAdjustsFontSizeToFitWidth:(BOOL)adjusts {
}

/**
@Status Stub
@Status Interoperable
*/
- (void)setMinimumScaleFactor:(float)scale {
_minimumScaleFactor = scale;
_useMinimumScaleFactor = YES;
}

/**
@Status Interoperable
*/
- (float)minimumScaleFactor {
return _minimumScaleFactor;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion bin/xib2nib.exe
Git LFS file not shown
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ - (void)viewDidLoad {
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return 33;
return 36;
}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
Expand Down Expand Up @@ -290,6 +290,25 @@ - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSI
[self _createLabelwithNumberOfLines:3 AdjustFontSizeToFitWidth:NO LineBreakMode:UILineBreakModeWordWrap MinimumFontSize:0.0f];
}

if (indexPath.row == 33) {
cell.accessoryView = [self _createLabelwithNumberOfLines:2
AdjustFontSizeToFitWidth:YES
LineBreakMode:UILineBreakModeTailTruncation
MinimumScaleFactor:0.0f];
}
if (indexPath.row == 34) {
cell.accessoryView = [self _createLabelwithNumberOfLines:2
AdjustFontSizeToFitWidth:YES
LineBreakMode:UILineBreakModeTailTruncation
MinimumScaleFactor:0.1];
}
if (indexPath.row == 35) {
cell.accessoryView = [self _createLabelwithNumberOfLines:2
AdjustFontSizeToFitWidth:YES
LineBreakMode:UILineBreakModeTailTruncation
MinimumScaleFactor:0.9f];
}

return cell;
}

Expand Down Expand Up @@ -319,4 +338,27 @@ - (UILabel*)_createLabelwithNumberOfLines:(int)numberOfLine
return textLabel;
}

- (UILabel*)_createLabelwithNumberOfLines:(int)numberOfLine
AdjustFontSizeToFitWidth:(BOOL)adjustFontSizeToFitWidth
LineBreakMode:(UILineBreakMode)lineBreakMode
MinimumScaleFactor:(float)minimumScaleFactor {
UILabel* textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, c_width, c_height)];
textLabel.numberOfLines = numberOfLine;
textLabel.adjustsFontSizeToFitWidth = adjustFontSizeToFitWidth;
textLabel.lineBreakMode = lineBreakMode;
textLabel.minimumScaleFactor = minimumScaleFactor;

BOOL shouldAdjustSize =
adjustFontSizeToFitWidth && (lineBreakMode != UILineBreakModeWordWrap) && (lineBreakMode != NSLineBreakByCharWrapping);

textLabel.text = [NSString
stringWithFormat:@"adjustFontSize = %d, numberOflines = %d, linbreakMode=%d, text %@ adjust to fit the width of this UIlabel",
textLabel.adjustsFontSizeToFitWidth,
textLabel.numberOfLines,
textLabel.lineBreakMode,
shouldAdjustSize ? @"should" : @"should not"];

return textLabel;
}

@end
32 changes: 13 additions & 19 deletions tools/vsimporter/xib2nib/UILabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ UILabel::UILabel() {
_textColor = NULL;
_numberOfLines = 1;
_baselineAdjustment = 0;
_adjustsFontSizeToFit = false;
_minimumFontSize = -1.0f;
_minimumScaleFactor = -1.0f;
_font = NULL;

// default line break mode is tailTrucation
Copy link
Contributor

@oliversa-msft oliversa-msft Feb 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: tailTruncation #Resolved

Expand All @@ -66,15 +66,16 @@ void UILabel::InitFromXIB(XIBObject* obj) {
_textAlignment = obj->GetInt("IBUITextAlignment", 0);
_numberOfLines = obj->GetInt("IBUINumberOfLines", 1);

// default line break mode is tailTrucation
// default line break mode is tailTruncation
_lineBreakMode = obj->GetInt("IBUILineBreakMode", 4);
_font = (UIFont*)obj->FindMember("IBUIFontDescription");
if (!_font)
_font = (UIFont*)obj->FindMember("IBUIFont");

if (FindMember("IBUIMinimumFontSize")) {
_minimumFontSize = FindMember("IBUIMinimumFontSize")->floatValue();
_adjustsFontSizeToFit = true;
} else if (FindMember("IBUIMinimumScaleFactor")) {
_minimumScaleFactor = FindMember("IBUIMinimumScaleFactor")->floatValue();
}

obj->_outputClassName = "UILabel";
Expand Down Expand Up @@ -138,13 +139,10 @@ void UILabel::InitFromStory(XIBObject* obj) {
}
}

if (getAttrib("minimumFontSize")) {
if (getAttrib("minimumScaleFactor")) {
_minimumScaleFactor = strtod(getAttrAndHandle("minimumScaleFactor"), NULL);
} else if (getAttrib("minimumFontSize")) {
_minimumFontSize = strtod(getAttrAndHandle("minimumFontSize"), NULL);
const char* adjust = getAttrAndHandle("adjustsFontSizeToFit");

if (!adjust || strcmp(adjust, "NO") != 0) {
_adjustsFontSizeToFit = true;
}
}

obj->_outputClassName = "UILabel";
Expand All @@ -170,8 +168,6 @@ void UILabel::ConvertStaticMappings(NIBWriter* writer, XIBObject* obj) {
obj->AddOutputMember(writer, "UIHighlightedColor", _highlightedColor);
}

AddOutputMember(writer, "UIMinimumScaleFactor", new XIBObjectFloat(1.0f));

if (_textAlignment != 0) {
AddInt(writer, "UITextAlignment", _textAlignment);
}
Expand All @@ -188,14 +184,12 @@ void UILabel::ConvertStaticMappings(NIBWriter* writer, XIBObject* obj) {
AddInt(writer, "UIBaselineAdjustment", _baselineAdjustment);
}

if (_numberOfLines == 1) {
if (_adjustsFontSizeToFit) {
AddBool(writer, "UIAdjustsFontSizeToFit", _adjustsFontSizeToFit);
}

if (_minimumFontSize != -1.0f) {
AddOutputMember(writer, "UIMinimumFontSize", new XIBObjectFloat(_minimumFontSize));
}
if (_minimumFontSize != -1.0f) {
AddOutputMember(writer, "UIMinimumFontSize", new XIBObjectFloat(_minimumFontSize));
} else if (_minimumScaleFactor != -1.0f) {
AddOutputMember(writer, "UIMinimumScaleFactor", new XIBObjectFloat(_minimumScaleFactor));
} else {
AddBool(writer, "UIAdjustsFontSizeToFit", false);
Copy link
Contributor

@oliversa-msft oliversa-msft Feb 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false [](start = 50, length = 5)

So we're changing it to always false in X2N (previous default was true). By design? #WontFix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no default for adjustFontToFit, it is either false or true. depending on whether or not you set minimum font size or minimum scale. if set, it is true. otherwise false. both xib and storyboard uses this contract.


In reply to: 99473218 [](ancestors = 99473218)

}

if (_font) {
Expand Down
18 changes: 8 additions & 10 deletions tools/vsimporter/xib2nib/UILabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,25 @@
class UIColor;
class UIFont;

class UILabel :
public UIView
{
class UILabel : public UIView {
private:
CGSize _shadowOffset;
const char *_text;
const char* _text;
UIColor *_textColor, *_highlightedColor;
UIFont *_font;
UIFont* _font;
int _textAlignment;
int _numberOfLines;
bool _adjustsFontSizeToFit;
float _minimumFontSize;
float _minimumScaleFactor;
int _baselineAdjustment;
int _lineBreakMode;

public:
UILabel();

virtual void InitFromXIB(XIBObject *obj);
virtual void InitFromStory(XIBObject *obj);
virtual void ConvertStaticMappings(NIBWriter *writer, XIBObject *obj);
ObjectConverter *Clone();
virtual void InitFromXIB(XIBObject* obj);
virtual void InitFromStory(XIBObject* obj);
virtual void ConvertStaticMappings(NIBWriter* writer, XIBObject* obj);
ObjectConverter* Clone();
};