Last active
April 3, 2022 08:52
-
-
Save davx1992/8f302e96820cee1a09dde82e00e0878c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** UI element heights which used in message height calculation */ | |
textPaddingVertical: number = 7; | |
textPaddingHorizontal: number = 10; | |
timeHeight: number = 12; | |
timeTopMargin: number = 5; | |
messageContainerVerticalMargin: number = 7; | |
dateSeperatorVerticalMargin: number = 20; | |
dateSeperatorHeight: number = 15; | |
imageHeight: number = 170; | |
imageBottomMargin: number = 10; | |
avatarHeight: number = 45; | |
windowWidth: number = Dimensions.get("window").width; | |
/** END */ | |
calculateMessageHeight(message: Message, index: number): number { | |
const textHeight = message.messageHeight; | |
let messageHeight = textHeight; | |
//If time seperator is shown | |
if ( | |
message && | |
!isSameDay( | |
message.created, | |
getSafe( | |
() => this.props.data.getDataForIndex(index + 1).created, | |
null | |
) | |
) | |
) { | |
messageHeight = | |
messageHeight + | |
this.dateSeperatorHeight + | |
this.dateSeperatorVerticalMargin * 2; | |
} | |
if (message.type === MessageType.IMAGE) { | |
messageHeight = | |
messageHeight + this.imageHeight + this.imageBottomMargin; | |
} | |
messageHeight = | |
messageHeight + | |
(this.timeHeight + this.timeTopMargin) + | |
this.textPaddingVertical * 2 + | |
this.messageContainerVerticalMargin * 2; | |
if (messageHeight < this.avatarHeight) { | |
messageHeight = this.avatarHeight; | |
} | |
return messageHeight; | |
} | |
layoutProvider = new LayoutProvider( | |
(index) => | |
this.props.data.getDataForIndex(index)?.type ?? MessageType.MESSAGE, | |
(type, dim, index) => { | |
const message = this.props.data.getDataForIndex(index); | |
dim.width = this.windowWidth - 15 * 2; | |
dim.height = this.calculateMessageHeight(message, index); | |
} | |
); | |
/* Here snippet how I calculate text height */ | |
import rnTextSize, { TSFontSpecs } from "react-native-text-size"; | |
import { Dimensions } from "react-native"; | |
const fontSpecs: TSFontSpecs = { | |
fontFamily: "Poppins-Regular", | |
fontSize: 13, | |
}; | |
const textPaddingHorizontal: number = 10; | |
const maxTextWidth: number = | |
Dimensions.get("window").width * 0.65 - textPaddingHorizontal * 2; | |
const textHeight = rnTextSize.flatHeights({ | |
...fontSpecs, | |
width: maxTextWidth, | |
text: [message.text], | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment