Skip to content
Merged
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
14 changes: 12 additions & 2 deletions nativescript-core/ui/text-base/text-base.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ export class TextBase extends TextBaseCommon {
const paragraphStyle = NSMutableParagraphStyle.alloc().init();
paragraphStyle.lineSpacing = this.lineHeight;
// make sure a possible previously set text alignment setting is not lost when line height is specified
paragraphStyle.alignment = (<UITextField | UITextView | UILabel>this.nativeTextViewProtected).textAlignment;
if (this.nativeTextViewProtected instanceof UIButton) {
paragraphStyle.alignment = (<UIButton>this.nativeTextViewProtected).titleLabel.textAlignment;
} else {
paragraphStyle.alignment = (<UITextField | UITextView | UILabel>this.nativeTextViewProtected).textAlignment;
}

if (this.nativeTextViewProtected instanceof UILabel) {
// make sure a possible previously set line break mode is not lost when line height is specified
paragraphStyle.lineBreakMode = this.nativeTextViewProtected.lineBreakMode;
Expand Down Expand Up @@ -192,7 +197,12 @@ export class TextBase extends TextBaseCommon {
const paragraphStyle = NSMutableParagraphStyle.alloc().init();
paragraphStyle.lineSpacing = style.lineHeight;
// make sure a possible previously set text alignment setting is not lost when line height is specified
paragraphStyle.alignment = (<UITextField | UITextView | UILabel>this.nativeTextViewProtected).textAlignment;
if (this.nativeTextViewProtected instanceof UIButton) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't we extract the logic of this if statement to a separate function or introduce some polymorphic behavior for UIButton as these lines are duplicated thus increasing the maintenance cost?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure it's a good idea in that case. It's only repeated twice and there is a lot of context.

Or maybe there is a bigger picture, but I don't see it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you think it's not a good idea and what do you mean by context?
The bigger picture is that, in my opinion, the codebase is starting to have a lot of code duplications and it's getting common to fix a bug at multiple places. To mitigate this problem I think we should be conservative about adding new code duplications and refactor existing ones whenever possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you are talking about rethinking this whole file to avoid even having this if statement, I agree. But moving just this if statement in a function to avoid repeating 2 lines of code, is a perfect example of over abstraction in my opinion.

However, I do want to say that I have little experience with nativescript, since I almost just discovered this project. So I don't pretend to have a genuine opinion in here. I'm just sharing my "general experience" on what looks like a lot of work for a not so important abstraction.

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright then, I'll approve your changes. We'd perhaps refactor this whole file so we'll abstract stuff at a later point. Thank you for the contribution :)

paragraphStyle.alignment = (<UIButton>this.nativeTextViewProtected).titleLabel.textAlignment;
} else {
paragraphStyle.alignment = (<UITextField | UITextView | UILabel>this.nativeTextViewProtected).textAlignment;
}

if (this.nativeTextViewProtected instanceof UILabel) {
// make sure a possible previously set line break mode is not lost when line height is specified
paragraphStyle.lineBreakMode = this.nativeTextViewProtected.lineBreakMode;
Expand Down