-
Notifications
You must be signed in to change notification settings - Fork 809
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -45,6 +47,9 @@ @implementation UILabel { | |
UILineBreakMode _lineBreakMode; | ||
BOOL _adjustFontSize; | ||
float _minimumFontSize; | ||
float _originalFontSize; | ||
BOOL _useMinimumScaleFactor; | ||
float _minimumScaleFactor; | ||
int _numberOfLines; | ||
BOOL _isDisabled; | ||
BOOL _isHighlighted; | ||
|
@@ -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 | ||
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 | ||
|
@@ -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])]; | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
nit: typo |
||
_adjustFontSize = NO; | ||
} | ||
} else { | ||
TraceWarning(TAG, L"Invalid nib format, None of UIMinimumScaleFactor/UIMinimumFontSize/UIAdjustsFontSizeToFit is set. default UIAdjustsFontSizeToFit to FALSE"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
nit defaulting |
||
_adjustFontSize = NO; | ||
} | ||
|
||
if ([coder containsValueForKey:@"UILineBreakMode"]) { | ||
_lineBreakMode = (UILineBreakMode)[coder decodeInt32ForKey:@"UILineBreakMode"]; | ||
|
@@ -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]; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -326,6 +356,7 @@ - (void)initAccessibility { | |
- (void)setFont:(UIFont*)font { | ||
if (![_font isEqual:font]) { | ||
_font = font; | ||
_originalFontSize = [_font pointSize]; | ||
if (_adjustFontSize) { | ||
[self _adjustFontSizeToFit]; | ||
} else { | ||
|
@@ -496,6 +527,7 @@ - (NSInteger)numberOfLines { | |
*/ | ||
- (void)setMinimumFontSize:(float)size { | ||
_minimumFontSize = size; | ||
_useMinimumScaleFactor = NO; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: tailTruncation #Resolved |
||
|
@@ -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"; | ||
|
@@ -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"; | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So we're changing it to always false in X2N (previous default was true). By design? #WontFix There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does the minimumFontSize property return a different value in this case? #Resolved
There was a problem hiding this comment.
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)